1

Is is possible to find how how many tags (and categories) a post has and to display it?

something like this: when a post has three Tags and two categories, at the end of the post show that number inside brackets, like so:

TAGS (3) | CATEGORIES (2)

I would like to have it so, because i would like that the tag/category list is hidden until the user clicks or hovers it.

Right now i am making the Tags into an unordered list like this:

the_tags('<ul><li>','</li><li>','</li></ul>');

thanks!

0

4 Answers 4

2

for tags you can use

$tags = get_tags();
$categories = get_categories();

$no_of_tags = count($tags);
$no_of_categories = count($categories);

refer this if you need more details : http://codex.wordpress.org/Function_Reference/get_tags http://codex.wordpress.org/Function_Reference/get_categories

1
  • That seems to give the number of total tags, NOT the number of tags for the post, as it was asked for Commented May 24, 2019 at 5:59
2

Use this,


$tags = wp_get_post_terms($post_id, 'post_tag');
        echo 'TAGS ('.count($tags).')';

$category = wp_get_post_terms($post_id, 'category');
        echo 'Category ('.count($category).')';

for reference http://codex.wordpress.org/Function_Reference/wp_get_post_terms

0

Use this code on your post loop: ya display on single.php

<?php
    while ( have_posts() ) : the_post();
        $tags = wp_get_post_terms($post->ID);
         echo 'TAGS ('.count($tags).')';
         $categories = get_categories();
         echo 'CATEGORIES ('.count($categories).')';
    endwhile;
?>

enter image description here

2
  • 1
    i have just noticed that with that code i was getting always for the categories te total amount of categories in the blog. it was all the same. I ended up using $categories = wp_get_post_terms($post->ID, "category"); Commented Jan 7, 2014 at 17:20
  • This code and above code both are right so you can any code use for your total.
    – Ravi Patel
    Commented Jan 8, 2014 at 4:10
0

Taking a little bit from all of you, this is what i ended up using:

$tags = wp_get_post_terms($post->ID);
echo 'TAGS ('.count($tags).')'

$categories = wp_get_post_terms($post->ID, "category");
echo 'CATEGORIES ('.count($categories).')'

Thank you all for your quick help! =)

Your Answer

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

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