1

I am trying to display tags related to a custom post type entry on the single page.

My Custom post type is registered as 'Blog' and the here is the code for the tags in my functions.php file:

function blog_tag_init() 
{

$labels = array(
    'name' => 'Tags',
    'singular_name' => 'Tag',
    'search_items' => 'Search Tags',
    'all_items' => 'All Tags',
    'parent_item' => 'Parent Tag',
    'parent_item_colon' => 'Parent Tag:',
    'edit_item' => 'Edit Tag',
    'update_item' => 'Update Tag',
    'add_new_item' => 'Add Tag',
    'new_item_name' => 'New Tag Name',
    'menu_name' => 'Tags',
);

register_taxonomy( 'blog-tags', 'blog',
    array(
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'hierarchical' => false,
        'query_var'         => true
    )
);
}
add_action( 'init', 'blog_tag_init' );

In my single template file I use the code below to display the tags:

<p><?php the_tags(' and tagged under ', ', '); ?> </p>

However the tags do not appear on the single page and I have no idea why because it all looks set up correctly.

1

You can't use the_tags in the custom post type. You should use the_terms instead.

So, you can use this function to get the tags:

the_terms( get_the_ID(), 'blog-tags', ' and tagged under ', ', '); 
  • unfortunately the tags do not appear after I change the code to your suggestion. – Neelam Khan Jun 19 '17 at 10:47
  • @NeelamKhan Sorry i had a mistype. I forgot to change post_tag to blog-tags. Also be sure the pass the ID, or use get_the_ID(). – Jack Johansson Jun 19 '17 at 10:48

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.