繁体   English   中英

WordPress-“ The Loop”显示所有帖子,而不是发布首页标题

[英]WordPress - 'The Loop' to show all posts, instead posts the title of the home page

我只是从一个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
?>

我不知道为什么它打印首页信息而不是帖子信息。

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

// 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM