【问题标题】:Cannot get post_content in customize page?无法在自定义页面中获取 post_content?
【发布时间】:2016-03-24 08:50:29
【问题描述】:

我创建了一个自定义页面,显示所有带有模板的帖子:

<?php
/*
Template Name: All posts
*/

get_header();
?>
</header>
<div role="main" id="content" class="content-warp">
    <div class="container">
        <div id="primary" class="content-area col-md-8 post-list">
            <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                    <div class="post">
                        <div class="entry">
                            <?php the_content(); ?>
                            <?php
                            $current_date = "";
                            $count_posts = wp_count_posts();
                            $nextpost = 0;
                            $published_posts = $count_posts->publish;
                            $myposts = get_posts(array('posts_per_page' => $published_posts));

                            foreach ($myposts as $post) :
                                get_template_part('content', 'content-single');
                                ?>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php endif; ?>
        </div>
        <?php get_sidebar('page'); ?>
        <div style="clear:both"></div>
    </div>
</div>
<?php get_footer(); ?>

但它不显示帖子的post_content(所有剩余数据都正常)。

顺便说一句,使用默认 UI 类别 (content.php),我只需调用下面的代码,一切正常:具有新模板但具有 post_content 的相同 UI。

&lt;?php get_template_part( 'content', 'single' ); ?&gt;

我不知道为什么 post_content 在我的新模板中为空。我正在使用 Llorix One LiteVersion:0.1.7

任何帮助,谢谢。

【问题讨论】:

    标签: php wordpress custom-wordpress-pages


    【解决方案1】:

    我尝试再次阅读 document 并意识到 the_content 尚未设置数据。 因此,只需添加设置帖子数据,如下所示:

    ...
    foreach($myposts as $post) : setup_postdata( $post );
    ...
    

    【讨论】:

      【解决方案2】:

      您也可以使用新的 WP 查询

      $myposts = new WP_QUery(array(
          'posts_per_page' => $published_posts
      ));
      
      if ($myposts->have_posts()): ?>
          <?php while ($myposts->have_posts()) {
                  $myposts->the_post();
      
                  get_template_part('content', 'content-single');
          } ?>
      <?php endif ?>
      
      <?php wp_reset_query(); ?>
      

      【讨论】:

        猜你喜欢
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        • 2012-12-28
        • 2022-01-27
        相关资源
        最近更新 更多