WordPress.org

Forums

Tag list with number of post related to tags (16 posts)

  1. dabigny
    Member
    Posted 7 years ago #

    Hi

    I want to display all the tags used in my wordpress along with the number of posts that use that tag on my blog. Any ideas on how to do this?

    I want it to display like this:
    Tag (12)
    Tag (2)
    Tag (23)

    Thanks in advance.

  2. Something like this will do it.

    $tags = get_tags(array(
    'orderby' => 'count',
    'order' => 'DESC',
    'number' => 30,
    ) );
    foreach ($tags as $tag) {
    echo $tag->name . '(' . $tag->count . ')';
    }
  3. dabigny
    Member
    Posted 7 years ago #

    Thanks. I will give it a try for sure and let you know.

  4. dabigny
    Member
    Posted 7 years ago #

    well it worked...kinda. i lost the linking when it displayed. now i know how to bring it back with some hard-coding but i wondered if there was an easier way.

  5. Well, you didn't say that you wanted it linked. You have to be specific about these sort of things.

    $tags = get_tags(array(
    'orderby' => 'count',
    'order' => 'DESC',
    'number' => 30,
    ) );
    foreach ($tags as $tag) {
    echo '<a href="' . get_tag_link($tag->term_id) . '" rel="tag">' . $tag->name . '</a>(' . $tag->count . ')<br />';
    }

    That will display them one per line. You just have to modify the echo line if you want them displayed differently.

  6. Anonymous
    Unregistered
    Posted 6 years ago #

    Hi Otto42,

    Thank you for this code. It mostly serves my needs, but I'm hoping there's a way to format the display of the tags in a list, so that the bullets in the theme I'm using are displayed.

    Here's a screen capture of how I incorporated your code into the PHP Code Widget:
    http://www.dotglu-review.com/public/screen_captures/tag_list_code.gif

    Here's a screen capture of the displayed tag list:
    http://www.dotglu-review.com/public/screen_captures/tag_list_display.gif

    You'll notice the tag list doesn't include the proper bulleted formatting.

    Any help you can offer is very much appreciated.

    Thank you!
    S

  7. tnerb
    Member
    Posted 6 years ago #

    Otto42, the code block you provided above is very helpful. I've tried wrapping your echoed statement in LI and having a conditional class inside the LI but I just can't get this to work.

    Can you tell us how to incorporate a conditional class such as below into your code block. Sorry if this is not as clear as it could be - I'm not a programmer.

    <li <?php if(is_tag('TheCurrentTag')){echo 'class="current-cat"';}?>>

  8. inform
    Member
    Posted 6 years ago #

    Ott042,

    This is great.

    Is there a way to have the numbers display in () as I have it setup? Currently the numbers are running into the tag name.

    You can see the action here:
    http://www.inform-ventures.com/blog/

    This is the code I am using from above:

    <?php $tags = get_tags(array(
    'orderby' => 'count',
    'order' => 'DESC',
    'number' => 30,
    ) );
    foreach ($tags as $tag) {
    echo '<li><a href="' . get_tag_link($tag->term_id) . '" rel="tag">' . $tag->name . $tag->count . '</a></li>';}; ?>
  9. inform
    Member
    Posted 6 years ago #

    Nevermind. Figured it out.

  10. Eric Amundson
    Volunteer Moderator
    Posted 6 years ago #

    Otto,

    Thanks a bunch for this code; it really helped me out. :)

    Eric

  11. Eric Amundson
    Volunteer Moderator
    Posted 6 years ago #

    I'm wondering if there's a way to omit certain tags, or better yet, only include tags that I specify? Similar to the wp_list_categories options.

  12. tnerb
    Member
    Posted 6 years ago #

    sewmyheadon: you can just add another line, like this and add the tags you wish to include...

    'include' => '1,2,3,4,5,6'

    Otto42: If you get the chance, would you please take a look at my question above. Thanks.

  13. Eric Amundson
    Volunteer Moderator
    Posted 6 years ago #

    tnerb, Thanks a bunch! Worked like a charm! :)

  14. zbrix
    Member
    Posted 6 years ago #

    inform could you tell me how you put the ()around tag count.
    thanks.

  15. inform
    Member
    Posted 6 years ago #

    @ zbrix

    See code below:
    '(' . $tag->count . ')'

  16. Eunen
    Member
    Posted 6 years ago #

    In what file does this code go in?

Topic Closed

This topic has been closed to new replies.

About this Topic