微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

方法

使用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>'
endif;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐