• Resolved sacconi

    (@sacconi)


    How could I add the number of posts for each category (in parenthesis) starting from this code (in archive.php):

    <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try this one:

    function custom_add_count_on_archive_title( $title ) {
     $term = get_queried_object();
     if( $term instanceof WP_Term && 'category' === $term->taxonomy ) {
      $title .= ' ('.$term->count.')';
     }
     return $title;
    }
    add_filter( 'get_the_archive_title', 'custom_add_count_on_archive_title', 10, 1 );

    Add this in the functions.php of your child-theme or via Code Snippet-plugin.

    Thread Starter sacconi

    (@sacconi)

    It works, thank you. Maybe is it possible to style the number and the parethesis by css?

    Replace

    $title .= ' ('.$term->count.')';

    with

    $title .= ' <span>('.$term->count.')</span>';

    and you should be able to style it via CSS, e.g.:

    .page-title span { color: red; }
    Thread Starter sacconi

    (@sacconi)

    Everything is working:)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding category count’ is closed to new replies.