0

What's the easiest way to add "count posts" to the category listing as seen as below?

<?php
if($categories){
    foreach($categories as $category) {
        $output .= '<li><a href="'.get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( esc_html__( 'View all posts in %s','themename' ), $category->name ) ) . '">' . $category->name . '</a></li>' . $separator;
    }
    echo trim($output, $separator);
}

1 Answer 1

0

I do not know exactly what you mean, but I think you need something like this?

$categories = get_terms( array( 'taxonomy' => 'category' ) );
if ( $categories ) { 
   $output = '';
   foreach( $categories as $category ) {
      $output .= '<li title="' . __( 'View all', 'themename' ) . ' ' . $category->count . ' ' . __( 'posts in', 'themename' ) . ' ' . $category->name . '">' . $category->name . ' ' . $separator . '</li>';
   }
   echo trim( $output, $separator );
}

Your Answer

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

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