最近在给客户做一个WordPress主题的时候,项目需要获取当前分类及其子分类下的文章总数,而且这里的分类还是自定义分类法下的分类,因为WordPress没有相关可以直接使用的自带函数,所以,为了实现这个功能,只能自己定义一个函数了。
那么这个自定义函数的定义方法如下:
//获取指定分类及其子分类下文章总数
function mx_get_term_postcount($id,$taxonomy) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => array( $id ),
'include_children' => true,
)
),
);
$the_query = new WP_Query( $args );
$count = $the_query->found_posts;
return $count;
wp_reset_postdata();
}
你只需要把上面的自定义函数放到您的functions.php中,然后在需要获取指定分类及其子分类下所有文章的地方使用下面的方法获取即可;
<?php echo mx_get_term_postcount($id,$taxonomy);?>
希望以上内容能对您有所帮助!如果有什么问题找不到答案,您也可以直接在本网站上进行提问,我们会尽快给您回复的。Related: mn twins attendance 2021, how to neutralize pink undertones in paint, bob einstein norm macdonald, scott commings wife, hearst membership credit card charge, vanderbilt indoor track meet 2022 results, ruston, la crime news, decommissioned bunkers for sale uk 2020, celebrate recovery locations, aladdin jokes dirty, sagittarius sun cancer moon pisces rising, products containing amyl nitrite, pelham middle school principal, twilight fanfiction bella and aro mates lemons, car accident cape coral yesterday,
还没有任何评论,你来说两句吧