WordPress - ' The Loop'显示所有帖子,而不是发布主页的标题

时间:2017-05-03 04:02:19

标签: php wordpress

我刚开始使用wordpress教程系列,其中的第一件事就是制作一个简单的"循环",以打印所有帖子的标题和说明。当我这样做时,它会打印主页的名称和描述。

<?php 
if ( have_posts() ) 
 {
  while ( have_posts() ) 
    {
        the_post(); 
        the_title('<h2><a href="the_permalink();"','</a></h2>');   
        the_content(); 
        // Post Content here
        //
    } // end while
 } // end if
?>

我无法弄清楚为什么它打印主页信息而不是发布信息。

1 个答案:

答案 0 :(得分:1)

要在任何页面上显示wordpress帖子,您需要在WP_Query中传递以下参数,然后通过对象循环它们。

// The Query
$the_query = new WP_Query( array( 'post_type' => 'post','posts_per_page' => -1 ) );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}