wordpress获取分类下文章列表的两个方法

2021年9月3日 飞飞 阅读(948)
知识付费主题,优惠促销中:xx-blog主题

方法一

使用query_posts()函数

$cats = get_categories();
foreach ( $cats as $cat ) {
query_posts( 'showposts=10&cat=' . $cat->cat_ID );
echo $cat->cat_name;
while ( have_posts() ) {
the_post();
the_title();
}
wp_reset_query();
}

showposts表示获取的数量,在执行完query_posts后,必须保证同时执行 wp_reset_query

方法二

使用 get_posts()函数,category获取某个分类下的文章,numberposts获取相应的数目。

$posts = get_posts( "category=4&numberposts=10" );
if( $posts ) :
foreach( $posts as $post ) : setup_postdata( $post );
echo '<a href=".the_permalink()." rel="bookmark" title=".the_title()".>".the_title().'</a>';
endforeach;
endif; 
wp_reset_query();


关注微信公众号『xx主题网

第一时间了解最新网络动态
关注博主不迷路~

未经允许不得转载:xx主题网 » wordpress获取分类下文章列表的两个方法
分享到:

评论抢沙发

要发表评论,您必须先

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

扫码关注微信公众号

扫描关注xx主题网