WordPress循环得到的帖子不是单页

时间:2016-10-11 14:12:00

标签: php wordpress loops templates

我正在尝试通过我正在为页面构建的模板页面显示最新帖子,循环没有运行最新帖子只有一页

好吧,我有一个简单的循环,获得最新的帖子

我的循环

            <?php
            if (have_posts()) : while (have_posts()) : the_post();
                get_template_part('content', get_post_format());
            endwhile; endif;
            ?>

和content.php

<div class="blog-post">

    <h2 class="blog-post-title">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </h2>

    <p class="blog-post-meta">
        <?php the_date(); ?>by <a href="#"><?php the_author(); ?></a>
        <a href="<?php comments_link(); ?>">
            <?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
        </a>
    </p>

    <?php if ( has_post_thumbnail() ) {?>
        <div class="row">
            <div class="col-md-4">
                <?php   the_post_thumbnail('thumbnail'); ?>
            </div>
            <div class="col-md-6">
                <?php the_excerpt(); ?>
            </div>
        </div>
    <?php } else { ?>
        <?php the_excerpt(); ?>
    <?php } ?>

</div>

当我在index.php中运行循环时,我得到了我最新的博文,完美。

然而,我正在构建一个模板页面,我尝试在这个页面中包含循环,我只得到一个页面(不是所有帖子)。

我的模板

    <div class="row">
        <div class="col-sm-12">
            // content bar
            <?php get_template_part('advicecentre_bar', get_post_format()) ?>

            // cmd driven content
            <?php
            if (have_posts()) : while (have_posts()) : the_post();
                get_template_part('content_page', get_post_format());
            endwhile; endif;
            ?>

            // recent post
            <?php
            if (have_posts()) : while (have_posts()) : the_post();
                get_template_part('content', get_post_format());
            endwhile; endif;
            ?>


        </div> <!-- /.col -->
    </div> <!-- /.row -->

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:2)

如果您在同一页面上使用多个循环,则必须使用rewind_posts(),如下所示:

<div class="row">
    <div class="col-sm-12">
        // content bar
        <?php get_template_part('advicecentre_bar', get_post_format()); ?>

        // cmd driven content
        <?php
        if (have_posts()) : while (have_posts()) : the_post();
            get_template_part('content_page', get_post_format());
        endwhile; endif;
        ?>

        <?php rewind_posts(); ?>

        // recent post
        <?php
        if (have_posts()) : while (have_posts()) : the_post();
            get_template_part('content', get_post_format());
        endwhile; endif;
        ?>


    </div> <!-- /.col -->
</div> <!-- /.row -->

<?php get_footer(); ?>

这会将循环“重置”为原始状态,并允许您再次查看帖子。在您的原始代码中,您扫描所有帖子,然后在第二个循环中扫描所有内容,因为您已经扫描了所有帖子!

答案 1 :(得分:0)

嗯我发现这个解决方案使用for for each而不是while循环似乎有效,但我不确定它是否是最好的方法。

             <ul>
                <?php
                $recent_posts = wp_get_recent_posts();
                foreach( $recent_posts as $recent ){
                    echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
                }
                wp_reset_query();
                ?>
            </ul>

更新

   <?php
    $args = array('numberposts' => 5);
    $recent_posts = wp_get_recent_posts($args);
    foreach ($recent_posts as $recent) {
        $excerpt = wp_trim_excerpt($recent['post_content']);
        $permalink = get_permalink($recent["ID"]);
        $title = esc_attr($recent["post_title"]);
        $thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
        echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
        echo $excerpt;
    }
    ?>