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.


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.

| improve this answer | |
  • You're a star. Thanks for the help, it's worked great. – Tom May 23 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.