admin管理员组文章数量:1303374
I need to display the current term in my custom taxonomy in a single post.
example:
- My custom taxonomy is products and term of them is product-1, product-2 and products-3.
- My post is assigned to product-2
- And I want to print the current products = products-2 in my post
In fact, I need a function like WordPress's the_category();
but for my taxonomy like the_customtaxonomy();
UPDATE :
in facts i know i need to get id of this becuse i need to show a icon for this in my single , for example a function like the_category_ID();
I need to display the current term in my custom taxonomy in a single post.
example:
- My custom taxonomy is products and term of them is product-1, product-2 and products-3.
- My post is assigned to product-2
- And I want to print the current products = products-2 in my post
In fact, I need a function like WordPress's the_category();
but for my taxonomy like the_customtaxonomy();
UPDATE :
in facts i know i need to get id of this becuse i need to show a icon for this in my single , for example a function like the_category_ID();
7 Answers
Reset to default 4You can use get_the_term_list()
:
Description
Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.
Usage
<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
tanks for answer from my friend , i find it for show slug of my taxonomy
<?php
$terms = get_terms('my-taxonomy-name');
foreach ( $terms as $term ) {
echo $term->slug.' ';
}
?>
and but it return all term in my taxonomy and i need to return current term in my taxonomy ..
UPDATE :
i finaly find this and add if for empty terms and works
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'oil' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->slug ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?>
I found it:
<?php
//list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
$taxonomy = 'genre';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
It gets all terms in my custom taxonomy and I need to get current term.
You need to use wp_get_object_terms()
wp_get_object_terms( $object_ids, $taxonomies, $args )
- $object_ids: string or array ids for the objects you want to get terms for
- $taxonomies: string or array of taxonomies
echo get_the_term_list( get_the_ID(), 'tax_name', 'Product:' );
Taking what user3208 coded, I have added a bit of code that adds the URL to the Term. Hope that helps someone out.
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'oil' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term, 'oil' );
// Print the name and URL
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
// Get rid of the other data stored in the object, since it's not needed
unset($term); } } ?>
A bit of update on the subject as this question is about 11 years old, and nobody mentioned it.
You can use get_query_var()
.
Retrieves the value of a query variable in the WP_Query class. to get the current term being queried on a taxonomy page.
Instead of doing a other query, we're just fetching the term from the current one. Then we can echoed it out on the front-end.
<?= get_query_var( 'term' ); ?>
本文标签: How to get current term in my custom taxonomy in WordPress
版权声明:本文标题:How to get current term in my custom taxonomy in WordPress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741672894a2391708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论