0

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

<?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(); ?>

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

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

<?php get_template_part( 'content', 'single' ); ?>

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

任何帮助,谢谢。

4

2 回答 2

0

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

...
foreach($myposts as $post) : setup_postdata( $post );
...
于 2016-03-24T09:13:03.743 回答
0

您也可以使用新的 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-03-24T09:18:59.267 回答