我也这样做过get_posts()
方法这是我用来在我的页面上获取帖子的代码。我根据帖子的类别筛选帖子,所以我加入了类别category_name
, 如果希望显示所有帖子,可以将其留空。
<?php $args = array(
\'posts_per_page\' => 5,
\'offset\' => 0,
\'category\' => \'\',
\'category_name\' => \'{category slug}\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'include\' => \'\',
\'exclude\' => \'\',
\'meta_key\' => \'\',
\'meta_value\' => \'\',
\'post_type\' => \'post\',
\'post_mime_type\' => \'\',
\'post_parent\' => \'\',
\'author\' => \'\',
\'post_status\' => \'publish\',
\'suppress_filters\' => true
);
$lastposts = get_posts( $args );
foreach($lastposts as $post) :
setup_postdata($post); ?>
<li<?php if ( $post->ID == $wp_query->post->ID )
{
echo \' class="current"\';
} else {} ?>>
<a href="<?php the_permalink() ?>"><?php
the_title();
?></a>
<?php
the_excerpt();
?>
</li>
<?php endforeach; ?>