admin管理员组文章数量:1303412
I have a loop to display the posts of my Custom Post Type 'workshop'. Inside the loop I display the related terms of two taxonomies 'area' and 'group' using wp_get_object_terms()
.
It works fine. However I'm struggling to control the order of the taxonomies when displayed. It should always be in first 'area' then 'group'.
The code to display the related taxonomies:
<?php $workshop_terms = wp_get_object_terms( $post->ID, array( 'area', 'group' ) );
if ( ! empty( $workshop_terms ) ) {
if ( ! is_wp_error( $workshop_terms ) ) {
foreach( $workshop_terms as $term ) {
echo esc_html( $term->name );
}
}
} ?>
Thank you.
I have a loop to display the posts of my Custom Post Type 'workshop'. Inside the loop I display the related terms of two taxonomies 'area' and 'group' using wp_get_object_terms()
.
It works fine. However I'm struggling to control the order of the taxonomies when displayed. It should always be in first 'area' then 'group'.
The code to display the related taxonomies:
<?php $workshop_terms = wp_get_object_terms( $post->ID, array( 'area', 'group' ) );
if ( ! empty( $workshop_terms ) ) {
if ( ! is_wp_error( $workshop_terms ) ) {
foreach( $workshop_terms as $term ) {
echo esc_html( $term->name );
}
}
} ?>
Thank you.
Share Improve this question asked Feb 15, 2021 at 10:31 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges1 Answer
Reset to default 0wp_get_object_terms()
accepts a third parameter which is an array of arguments passed to WP_Term_Query::get_terms
which runs the actual SQL queries for the specific term request, and one of the arguments is named orderby
which accepts term fields like name
and taxonomy
.
So you can use that 3rd parameter and set the orderby
to taxonomy
(and order
to ASC
) like so:
$workshop_terms = wp_get_object_terms( $post->ID, array( 'area', 'group' ), array(
'orderby' => 'taxonomy',
'order' => 'ASC',
) );
Or the other way, is of course, call wp_get_object_terms()
once for each of the taxonomies... ✌
本文标签: custom post typesReturn multiples taxonomies with wpgetobjectterms
版权声明:本文标题:custom post types - Return multiples taxonomies with wp_get_object_terms 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741749077a2395714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论