【问题标题】:displaying standard WP get_content with 'Advanced Custom Fields'显示带有“高级自定义字段”的标准 WP get_content
【发布时间】:2014-10-23 16:47:53
【问题描述】:

我有以下使用高级自定义字段 (ACF) 的循环 - 除内容外,所有内容均显示。我尝试了各种解决方案,但都没有成功。

<?php
          $posts = get_posts(array(
            'numberposts' => -1,
            'post_type' => 'sectors',
            'orderby' => 'menu_order',
            'order' => 'ASC'
          ));
          global $post;
          if($posts) {
            ?>

            <?php
            foreach($posts as $post) {
              ?>
              <div class="content full sector">
                        <?php the_post_thumbnail(); ?>
                        <h2><?php the_title(); ?></h2>
                        <p><?php the_content(); ?></p>
                    </div>
              <?php
            }
            ?>

             <?php
          }
          wp_reset_postdata();
        ?>

非常感谢任何帮助。

谢谢

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    当你使用get_posts()返回post数据时,一些post相关的数据默认是不可用的。其中一项是the_content()。这可以通过调用内部函数setup_postdata() 来解决,并将$post 数组作为其参数。

    解决方案:在您的foreach() 中添加setup_postdata( $post );。

    阅读更多in the Codex。

    【讨论】:

    • 太好了,请将此答案选为正确,以将您的问题标记为已解决。
    猜你喜欢
    • 2017-05-01
    • 2019-01-18
    • 2018-08-26
    • 2016-07-17
    • 2018-04-15
    • 1970-01-01
    • 2021-02-11
    • 2019-08-20
    • 2014-10-13
    相关资源
    最近更新 更多