如果你想这样做的话,你可以抓住一个术语。
使用get_term_by()
:
$term = get_term_by( \'name\', \'Term1\', \'mytax\' );
应提供
WP_Term
名称为的术语的
Term1
在分类学中
mytax
.
[编辑]Get the first term:
$terms = wp_get_post_terms( $post->ID, \'mytax\', array( "fields" => "all" ) );
if ( ! empty( $terms ) ) {
$term = $terms[0];
$term_id = $terms[0]->term_id; // If you just need the term ID.
}
这将从原始问题列表中获取第一个术语。但是,请注意,默认情况下,
wp_get_post_terms()
按其升序对术语进行排序
name
领域
You can modify this using order
and orderby
in the $args
parameter:
$args = array(
\'fields\' => \'all\',
\'orderby\' => \'slug\',
\'order\' => \'DESC\',
// ...for example.
);
$terms = wp_get_post_terms( $post->ID, \'mytax\', $args ) );
if ( ! empty( $terms ) ) {
$term = $terms[0];
$term_id = $terms[0]->term_id; // If you just need the term ID.
}