admin管理员组文章数量:1315290
I need help accessing a specific term from a custom taxonomy.
I am getting the terms with
$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));
If I print_r($terms)
this is what I get:
Array (
[0] => WP_Term Object (
[term_id] => 30
[name] => Term1
[slug] => term1
[term_group] => 0
[term_taxonomy_id] => 30
[taxonomy] => mytax
[description] =>
[parent] => 0
[count] => 78
[filter] => raw
)
[1] => WP_Term Object (
[term_id] => 32
[name] => Term2
[slug] => term2
[term_group] => 0
[term_taxonomy_id] => 32
[taxonomy] => mytax
[description] =>
[parent] => 30
[count] => 44
[filter] => raw
)
)
How do I extract the ID for Term1 out of this array?
I need help accessing a specific term from a custom taxonomy.
I am getting the terms with
$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));
If I print_r($terms)
this is what I get:
Array (
[0] => WP_Term Object (
[term_id] => 30
[name] => Term1
[slug] => term1
[term_group] => 0
[term_taxonomy_id] => 30
[taxonomy] => mytax
[description] =>
[parent] => 0
[count] => 78
[filter] => raw
)
[1] => WP_Term Object (
[term_id] => 32
[name] => Term2
[slug] => term2
[term_group] => 0
[term_taxonomy_id] => 32
[taxonomy] => mytax
[description] =>
[parent] => 30
[count] => 44
[filter] => raw
)
)
How do I extract the ID for Term1 out of this array?
Share Improve this question edited Jun 19, 2018 at 20:18 Pat J 12.4k2 gold badges28 silver badges36 bronze badges asked Jun 19, 2018 at 20:09 Philipp KPhilipp K 1931 gold badge3 silver badges10 bronze badges2 Answers
Reset to default 3It's a little bit hard to be sure, what are you asking exactly, but... Let me try to answer...
So somewhere in single.php
you're getting terms for current post using this code:
$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));
and you want to get the ID of first term from that list?
If so, you can achieve this with this code:
$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));
$term_id = false;
if ( $terms ) {
$term_id = $terms[0]->term_id;
}
You can just grab a single term, if that's what you want to do.
Using get_term_by()
:
$term = get_term_by( 'name', 'Term1', 'mytax' );
should deliver the WP_Term
object for the term named Term1
in the taxonomy mytax
.
[edit] 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.
}
This will grab the first term from the list you're getting in the original question. Be aware, though, that by default, wp_get_post_terms()
sorts the terms in ascending order by their name
field. 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.
}
本文标签: How do I access a single term from a post
版权声明:本文标题:How do I access a single term from a post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741941262a2406158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论