Forums

[resolved] Single Tag and Single Category count in Archive page (5 posts)

  1. further
    Member
    Posted 3 years ago #

    Hello!

    I'd like to get a post count on tags and categories once I open the archive page, next to the tag or category name.

    You can see an example of what i mean here:

    http://www.elblogalternativo.com/author/angel-de-la-guarda/
    (Los artículos de Angel de la Guarda (154):)

    I managed to get the category and tag name (single_cat_title and single_tag_title), but i've not found a tread dealing with this issue.

    Many thanks in advance!

    Erik.

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Might be more than you need but try this in your archive.php:

    <?php
    $term_id='';
      if ( is_tag() ) {
        $term_id = get_query_var('tag_id');
        $taxonomy = 'post_tag';
        $title = 'Tag: ';
      }
      if ( is_category() ) {
        $term_id = get_query_var('cat');
        $taxonomy = 'category';
        $title = 'Category: ';
      }
    if ($term_id) {
      $args ='include=' . $term_id;
      $terms = get_terms( $taxonomy, $args );
      if ($terms) {
        foreach($terms as $term) {
          if ($term->count > 0) {
            echo '<p>' . $title . '<a href="' . get_term_link( $term->term_id, $taxonomy ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
          }
        }
      }
    }
    ?>
  3. further
    Member
    Posted 3 years ago #

    MichaelH, it works perfectly!

    I spent like 3 hours last week looking into the forums for a solution like this one and I found several other people with the same issue and no answer, you might wanna consider publishing it into the codex or something, it works fantastic!

    Many thanks,

    Erik.

  4. MichaelH
    Volunteer
    Posted 3 years ago #

    Eric - can you recommend a place in Codex to put that? Thanks.

  5. gerbus3
    Member
    Posted 2 years ago #

    Agreed, that's beautiful and rare.

Topic Closed

This topic has been closed to new replies.

About this Topic