[英]wordpress output the content of a static home page instead of all available posts
[英]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.