无法从ajax调用中获取发布数据 - wordpress

时间:2022-10-18 07:32:16

I have an Ajax call loading more posts onto a page. Everything works and I get the HTML back from the template that I have but it's missing all the of post information so it's just the html skeleton.

我有一个Ajax调用将更多帖子加载到页面上。一切正常,我从我拥有的模板中取回HTML,但它缺少所有帖子信息,所以它只是html骨架。

I'm using the same template to load the first posts onto the page but then when using the ajax call, I can't get the Wordpress methods to display back the data.

我使用相同的模板将第一个帖子加载到页面上,但是当使用ajax调用时,我无法使用Wordpress方法显示数据。

When I var dump on my template php file, I can see all of the post information but it doesn't come through back to the inline php.

当我在我的模板php文件上转储var时,我可以看到所有的帖子信息,但它没有回到内联php。

Loading the Posts:

加载帖子:

foreach (load_posts($offset) as $post) {
    include(locate_template('_includes/loop-archive-single.php'));
} 

The Template:

<li>
    <div class="card">
        <div class="card__bd">
            <div class="feature">
                <div class="feature__media">
                    <a class="imgLink" href="<?php the_permalink(); ?>">
                        <img src="<?php the_field('media_image') ?>" alt="<?php the_field('media_image') ?>" />
                    </a>
                </div>
                <div class="feature__hd">
                    <h2 class="hdg hdg--lg">
                       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </h2>
                </div>
                <div class="feature__bd">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        </div>
    </div>
</li>

Am I outside the loop or what's going on? I'd like to keep the template that way it is right now. Again, the post data is there, but it just won't show through the_permalink() or the_field('media_image') and anything like that...

我是在循环之外还是在发生什么?我想以现在的方式保留模板。同样,帖子数据在那里,但它不会通过the_permalink()或the_field('media_image')和类似的东西显示......

1 个解决方案

#1


0  

You will have to pass the ID on ajax call ... of which post you need

您必须在ajax调用中传递ID ...您需要哪个帖子

change the template file to

将模板文件更改为

<li>
    <div class="card">
        <div class="card__bd">
            <div class="feature">
                <div class="feature__media">
                    <a class="imgLink" href="<?php the_permalink(); ?>">
                        <img src="<?php the_field('media_image',$post_ID) ?>" alt="<?php the_field('media_image',$postID) ?>" />
                    </a>
                </div>
                <div class="feature__hd">
                    <h2 class="hdg hdg--lg">
                       <a href="<?php the_permalink($postID); ?>"><?php the_title(); ?></a>
                    </h2>
                </div>
                <div class="feature__bd">
                    <?php the_excerpt($postID); ?>
                </div>
            </div>
        </div>
    </div>
</li>

#1


0  

You will have to pass the ID on ajax call ... of which post you need

您必须在ajax调用中传递ID ...您需要哪个帖子

change the template file to

将模板文件更改为

<li>
    <div class="card">
        <div class="card__bd">
            <div class="feature">
                <div class="feature__media">
                    <a class="imgLink" href="<?php the_permalink(); ?>">
                        <img src="<?php the_field('media_image',$post_ID) ?>" alt="<?php the_field('media_image',$postID) ?>" />
                    </a>
                </div>
                <div class="feature__hd">
                    <h2 class="hdg hdg--lg">
                       <a href="<?php the_permalink($postID); ?>"><?php the_title(); ?></a>
                    </h2>
                </div>
                <div class="feature__bd">
                    <?php the_excerpt($postID); ?>
                </div>
            </div>
        </div>
    </div>
</li>