如果我想知道10个不同标签中总共有多少帖子,我该怎么写?
如果你是指职位总数的总和,即count
每个术语的值,然后:
是的,您可以将slug列表放入一个数组中,并循环遍历各个项目,以手动求和总计:
$term_slugs = [ \'slug\', \'slug-2\', \'etc\' ];
$post_count = 0;
foreach ( $term_slugs as $slug ) {
$term = get_term_by( \'slug\', $slug, \'post_tag\' );
$post_count += $term->count;
// This is just for testing.
echo $term->name . \': \' . $term->count . \'<br>\';
}
echo \'Grand total posts: \' . $post_count;
或者,如果您只需要总计,那么您可以使用
get_terms()
或
get_tags()
(使用
get_terms()
顺便说一句)带
wp_list_pluck()
无需执行
foreach
循环:
$term_slugs = [ \'slug\', \'slug-2\', \'etc\' ];
$terms = get_terms( [
\'taxonomy\' => \'post_tag\',
\'slug\' => $term_slugs,
] );
/* Or with get_tags():
$terms = get_tags( [ \'slug\' => $term_slugs ] );
*/
$post_count = array_sum( wp_list_pluck( $terms, \'count\' ) );
echo \'Grand total posts: \' . $post_count;