WordPress - 显示所有帖子的“The Loop”,而不是发布主页的标题
问题描述:
I just started with a wordpress tutorial series and one of the first things it does was make a simple "the loop", to print the title and description of all of the posts. When I did it though, it prints the name and description of the home page.
<?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
?>
I cannot figure out why it prints the home page information instead of post info.
我刚开始使用wordpress教程系列,其中的第一件事就是做一个简单的“循环” ,打印所有帖子的标题和说明。 当我这样做时,它会输出主页的名称和描述。 p>
&lt;?php
if(have_posts())
{
while(have_posts) ())
{
the_post();
the_title('&lt; h2&gt;&lt; a href =“the_permalink();”','&lt; / a&gt;&lt; / h2&gt;');
the_content();
//在此处发布内容
//
} //结束时
} //结束如果
?&gt;
code> pre>
我无法想象 为什么它打印主页信息而不是发布信息。 p>
div>
答
To display wordpress posts on any page , you need to pass the arguments as the following in the WP_Query and then loop them via 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
}