Php WordPress循环获取文章而不是单页

Php WordPress循环获取文章而不是单页,php,wordpress,loops,templates,Php,Wordpress,Loops,Templates,我试图通过我为页面构建的模板页面来显示最新的帖子,循环没有运行最新的帖子,只有一个页面 好的,我有一个简单的循环来获取最新的帖子 我的循环 <?php if (have_posts()) : while (have_posts()) : the_post(); get_template_part('content', get_post_format()); endwhile; endif

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

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

我的循环

            <?php
            if (have_posts()) : while (have_posts()) : the_post();
                get_template_part('content', get_post_format());
            endwhile; endif;
            ?>
<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>

和content.php

            <?php
            if (have_posts()) : while (have_posts()) : the_post();
                get_template_part('content', get_post_format());
            endwhile; endif;
            ?>
<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中运行循环时,我得到了我的最新博文,perfect

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

我的模板

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

//内容栏
//cmd驱动的内容
//最近的职位

如果在同一页上使用多个循环,则必须使用以下类似的方法:


//内容栏
//cmd驱动的内容
//最近的职位

这将“重置”循环到其原始状态,并允许您再次查看帖子。在原始代码中,你扫描了所有的帖子,然后在第二个循环中,什么也不扫描,因为你已经扫描了所有的帖子

嗯,我发现使用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;
    }
    ?>

Hmm有趣,+1帮助我理解。然而,这并不能解决我的问题。我仍然只看到在show上工作的页面作为循环输出,而不是所有最近的帖子