我试图在我的wordpress博客上只显示“最新”类别的帖子,但奇怪的是,同样的帖子竟然显示了两次。我做错了什么?
/*
* Theme file:index.php
*/
global $post;
$categories=get_categories();
foreach($categories as $categories_item)
{
if(strcasecmp($categories_item->name, \'latest\') == 0)//case-insensitive string comparison
{
$args = array(
\'numberposts\' => 2,
\'category\' => $categories_item->cat_ID,
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'post_status\' => \'publish\'
);
$latest_stuff = get_posts( $args );
echo "<h2>Latest stuff</h2>";
foreach($latest_stuff as $latest_stuff_item)
{
setup_postdata($post);
echo "<div>".the_content()."<div><hr>";
}
}
}
SO网友:Lee
问题是您正在使用echo the_content();
.
这会导致循环输出出现在循环外部。如果你改变the_content()
到get_the_content()
这将起作用。
注意:您可能需要将ID传递到get_the_content()
通过使用$latest_stuff_item->ID
foreach($latest_stuff as $latest_stuff_item)
{
setup_postdata($post);
echo "<div>".get_the_content()."<div><hr>";
}