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.

1 Answer 1

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.

1
  • You're a star. Thanks for the help, it's worked great. Commented May 23, 2020 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.