WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Counting how many posts are filed under one tag (3 posts)

  1. Nick
    Member
    Posted 6 years ago #

    Hi!

    On the archive page, I want to display how many posts are covered by the relevant category / tag archive.

    The following code shows the number of posts of a category:

    <div class="archive_number">
    This category contains <?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->category_count; ?> posts
    </div>

    Now, I want to display the number of posts filed under a tag. I've tried it with the following code:

    <div class="archive_number">
    <?php $tag = get_query_var('tag_id'); $tag = $tag[0]; echo $tag->tag_count; ?> posts are filed under this tag.
    </div>

    But (of course) this does not work. Any ideas what I do wrong? Be patient, I'm a php n00b.

    Thanks for your help.

  2. Kafkaesqui

    Posted 6 years ago #

    Access it through the queried object on the page:

    <?php
    $tag = $wp_query->get_queried_object();
    echo $tag->count;
    ?>
  3. Nick
    Member
    Posted 6 years ago #

    Perfect!

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic