这应该可以奏效。
<?php
// start with an empty array
$all_term = array();
if ( have_posts() ) {
// loop through all posts, this could go outside of the main loop or be part of it, if placing outside the main loop be sure to reset the loop or namespace your queries
while ( have_posts() ) {
the_post();
// add the list of terms for a given taxonomy to the $all_term array
array_push($all_term, wp_get_post_terms($post->ID, \'my_taxonomy\', array("fields" => "ids")));
} // end while
} // end if
// remove duplicates from the $all_term array
$all_term = array_unique($all_term, SORT_REGULAR);
// output the result
echo count($all_term);
?>