听起来你在尝试获取一篇文章的分类术语。类别术语不存储在post meta中。
你可以使用get_the_terms()
. 在本例中,$terms
将为category
与当前帖子关联的分类。您可以替换category
使用自定义分类名称。
$terms = get_the_terms( get_the_ID(), \'category\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo \'<ul>\';
foreach ( $terms as $term ) {
echo \'<li>\' . $term->name . \'</li>\';
}
echo \'</ul>\';
}
当我们在上面的代码中迭代foreach循环时,每个term对象将如下所示:
WP_Term Object
(
[term_id] => 2
[name] => Alignment
[slug] => alignment
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] => Posts in this category test image and text alignment.
[parent] => 0
[count] => 4
[filter] => raw
)