2017-05-03 90 views
0

我刚开始使用wordpress教程系列,它的第一件事之一是做一个简单的“循环”,打印所有帖子的标题和描述。当我这样做时,它会打印主页的名称和说明。WordPress - 'The Loop'显示所有帖子,而不是帖子标题主页

<?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 
?> 

我不明白为什么它打印主页信息,而不是发布信息。

+3

您是在哪里放置该代码的?如果你想显示所有的帖子标题和内容,你应该考虑全局变量$ post。 – heero

+0

这是整个索引文件。我正在观看一个视频教程,他们能够用它来解决这个问题。 –

回答

0

要在任何页面上显示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 
} 
+0

好吧,这更有意义。我想知道这个帖子是从哪里得到的。我只是有点困惑,因为我在做一个视频教程,而不需要制作一个新的对象。 –

+0

实际上,the_post默认会首先获取首页(通常是您的主页)的信息。如果您想在主页上显示您的帖子,可以通过将设置面板中的阅读选项更改为博客来实现。 –

相关问题