nimblesage
nimblesage

Reputation: 25

Display post count in each category - wordpress

The code below displays correctly except for the part that is suppose to show post count in the respective categories. What will be to correct syntax to use? Thanks!

<ul class="cat-menu list-group">
 <?php $category_ids = get_all_category_ids();

  $args = array(
     'orderby' => 'slug',
     'parent' => 0
  );
  $categories = get_categories( $args );
  foreach ( $categories as $category ) {
     echo '<li class="list-group-item d-flex align-items-center"><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $the_query->found_posts . '</span></a></li>';
  }
 ?>

Upvotes: 2

Views: 9297

Answers (2)

yawar
yawar

Reputation: 21

Very thanks @Arif-Khan.

and it is a simple update for persian Websites.

            <div class="card card-body pb-0">
                <div class="single-post">

                    <p class="font-bold dark-grey-text text-center spacing grey lighten-4 py-2 mb-4">
                         <strong>دسته بندی</strong>
                    </p>
                        <ul class="cat-menu list-group my-4">
                            <?php
                                $args = array(
                                    'orderby' => 'slug',
                                    'parent' => 0
                                );
                                $categories = get_categories( $args );
                                foreach( $categories as $category ){
                                    echo '<li class="list-group-item d-flex justify-content-between align-items-center"><a href="' . get_category_link( $category->term_id ) . '">
                                    <i class="fas fa-chevron-left blue-text" aria-hidden="true"></i><p class="blue-grey-text mb-0 pr-1 d-inline-block">
                                    ' . $category->name . '</p></a>
                                    <span class="badge badge-pill blue-grey font-small">' . $category->category_count . '</span></li>';
                                }
                            ?>
                        </ul>
                </div>
        </div>

Upvotes: 1

Arif Khan
Arif Khan

Reputation: 508

Try this-

<ul class="cat-menu list-group">
    <?php
        $args = array(
            'orderby' => 'slug',
            'parent' => 0
        );
        $categories = get_categories( $args );
        foreach( $categories as $category ){
            echo '<li class="list-group-item d-flex align-items-center"><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $category->category_count . '</span></a></li>';
        }
    ?>
    </ul>

Upvotes: 2

Related Questions