admin管理员组文章数量:1336289
On my WordPress site I have created a custom taxonomy named "game" which lists different gaming titles. I want to be able to display the names of these titles on my posts using a shortcode.
Prior to creating a custom taxonomy, I was using WordPress' default categories taxonomy, and had the following code (found on stack exchange) working well:
function shortcode_post_category () {
$html = '';
$categories = get_the_category();
foreach( $categories as $category ){
$html .= '<h2>' . strtoupper($category->name) . ' KEYBINDS</h2>';
}
return $html;
}
add_shortcode( 'category-keybinds', 'shortcode_post_category' );
From what I understand I need to change the get_the_categories
to get_the_term
(?) however with my very limited knowledge of php
I cannot seem to get this to work on the new "game" taxonomy.
On my WordPress site I have created a custom taxonomy named "game" which lists different gaming titles. I want to be able to display the names of these titles on my posts using a shortcode.
Prior to creating a custom taxonomy, I was using WordPress' default categories taxonomy, and had the following code (found on stack exchange) working well:
function shortcode_post_category () {
$html = '';
$categories = get_the_category();
foreach( $categories as $category ){
$html .= '<h2>' . strtoupper($category->name) . ' KEYBINDS</h2>';
}
return $html;
}
add_shortcode( 'category-keybinds', 'shortcode_post_category' );
From what I understand I need to change the get_the_categories
to get_the_term
(?) however with my very limited knowledge of php
I cannot seem to get this to work on the new "game" taxonomy.
1 Answer
Reset to default 0If you look at the source code of get_the_category()
you see that this is not much more than a call to get_the_terms ( $id, 'category' )
, where $id
is the current post id.
So, what you need is get_the_terms ( get_the_ID(), 'game' )
. You need get_the_ID
, because the ID
is not passed to the shortcode as it is to get_the_category
.
本文标签: customizationDisplay Custom Taxonomy Name As A Shortcode
版权声明:本文标题:customization - Display Custom Taxonomy Name As A Shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406768a2468980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论