admin管理员组文章数量:1122826
Taxonomy name is "intro". I'm inside a shortcode so I dont need to echo.I need a variable for the selected taxonomy term to put in the return. What I do has no effect
$terms2 = wp_get_post_terms($post->ID,'intro',array('fields' => 'names'));
Taxonomy name is "intro". I'm inside a shortcode so I dont need to echo.I need a variable for the selected taxonomy term to put in the return. What I do has no effect
$terms2 = wp_get_post_terms($post->ID,'intro',array('fields' => 'names'));
Share
Improve this question
asked Mar 30, 2024 at 9:42
Andrea SacconiAndrea Sacconi
797 bronze badges
2 Answers
Reset to default 1This is the solution:
$term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;
If you want to retrieve the selected term of a custom taxonomy called "intro" for a particular post inside a shortcode, you can use the wp_get_post_terms()
function to fetch the terms associated with that post
function my_custom_shortcode_function() {
global $post;
// Get the terms for the "intro" taxonomy associated with the current post
$terms = wp_get_post_terms($post->ID, 'intro', array('fields' => 'names'));
// Check if any terms are retrieved
if (!empty($terms)) {
// Assuming you want to use the first term only
$selected_term = $terms[0];
} else {
// If no terms are retrieved, set a default value or handle the case as needed
$selected_term = 'No Term Selected';
}
// Now you can use $selected_term as needed
return $selected_term;
}
add_shortcode('my_shortcode', 'my_custom_shortcode_function');
本文标签: phphow can I get the selected term of a custom taxonomy
版权声明:本文标题:php - how can I get the selected term of a custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312155a1934998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论