虽然我不能保证这就是您想要的(因为您还没有发布您的预期输出),但希望这会有所帮助。
即使它不完全是您想要的,您也可以使用每个术语的整个对象,以便您可以完全按照自己的意愿操作输出。
/** Grab the terms within the for the \'portfolio_cat\' taxonomy for the current post */
$terms = wp_get_object_terms($post->ID, \'portfolio_cat\'); // If you are in The Loop you can replace \'$post->ID\' with \'get_the_ID()\'
/** Loop through each term and add them to an array */
$formatted_terms = array();
foreach($terms as $term) :
$formatted_terms[] = sprintf(
\'<a href="urlhere/%1$s" title="View posts in $2%s">%3$s</a>\',
$term->slug, /** %1$s - The term slug (for the \'href\' attribute)*/
esc_attr($term->name), /** %2$s - The term name (formatted for the \'title\' attribute) */
$term->name /** %1$s - The term name (for display) */
);
endforeach;
/** Output the terms, formatted as you desire */
echo join(\',\', $formatted_terms);
用于实现所需输出的功能是
wp_get_object_terms()
. 如果需要,可以将第三个参数作为包含参数的数组传递,以对术语进行排序并仅重新运行某些字段,因此请查看
Function Reference.
代码位于$temrs
以对象数组的形式返回,如果要使用为每个术语返回的任何其他字段,请参阅对象的格式-
[0] => stdClass Object
(
[term_id] => 22
[name] => My Taxonomy Term
[slug] => my-taxonomy-term
[term_group] => 0
[term_taxonomy_id] => 29
[taxonomy] => portfolio_cat
[description] =>
[parent] => 20
[count] => 5
[filter] => raw
)