【问题标题】:Not able to get post data from ajax call - wordpress无法从 ajax 调用中获取发布数据 - wordpress
【发布时间】:2016-04-17 16:07:32
【问题描述】:

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

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

当我在我的模板 php 文件上进行 var dump 时,我可以看到所有的帖子信息,但它不会返回到内联 php。

加载帖子:

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

模板:

<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>

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

【问题讨论】:

  • 确认!你为什么不使用get_template_part?另外,你在做 AJAX WordPress Way 吗?最后,load_posts 有什么收获吗?这不是 WP 功能(我知道),所以我强烈怀疑你在 WordPress 结构之外做事。

标签: php ajax wordpress


【解决方案1】:

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

把模板文件改成

<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>

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多