最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Display Custom Taxonomy Name As A Shortcode

programmeradmin0浏览0评论

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.

Share Improve this question edited May 23, 2020 at 16:25 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 23, 2020 at 16:12 TomTom 231 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If 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.

发布评论

评论列表(0)

  1. 暂无评论