Share this Article !

How to loop post under category in wordpress

To loop posts under categories, you should know something.

  • get_categories($args): Arguments to retrieve categories and we want to return array List of category objects that   ‘parent’ => 0 .
 $categories = get_categories(array(
             ‘parent’ => 0,
‘order’=>’DESC’,
             ));
  • foreach ( $categoriesas$category ): loop category , show category name and add herf for category link <?php echo get_category_link($category->term_id )?>
    count total post <?php echo$category->count?> under parent category
  • query post under ‘cat’=>$category->cat_ID then loop post  foreach( $the_query as $post )
    and set post per page or ‘numberposts’=>4

follow Code as below:

<main class="container">
    <div class="row catparent-list my-4">
        <div class="col-lg-8 col-md-8 h-100">
            <?php
            global $post;
            $categories = get_categories(array(
                'parent' => 0,
                'order' => 'DESC',
            ));
            foreach ($categories as $category) :
            ?>
                <div class="mb-3 border-bottom py-3 ">
                    <a class="border bg-light d-block p-3 rounded mb-3 text-uppercase" href="<?php echo get_category_link($category->term_id) ?>">
                        <?php echo $category->name ?> <span class="badge rounded-pill bg-primary" title="total post"><?php echo $category->count ?>
                            <i class="fa fa-sticky-note" aria-hidden="true"></i></span>
                    </a>
                    <p><?php echo $category->description ?></p>
                    <?php
                    $args = array(
                        'numberposts' => 4,
                        'offset' => 0,
                        'cat' => $category->cat_ID
                    );
                    $the_query = get_posts($args);
                    if ($the_query) : ?>
                        <div class="row">
                            <?php
                            foreach ($the_query as $post) :
                            ?>
                                <div class="col-lg-6 col-md-6 mb-3">
                                    <a href="<?php the_permalink(); ?>">
                                        <div class="card h-100">
                                            <?php getthumbnail(); ?>
                                            <div class="card-body">
                                                <div class="card-title">
                                                    <?php the_title(); ?>
                                                </div>
                                                <div class=" card-text card-description hidden-phone">
                                                    <?php the_excerpt(); ?>
                                                </div>
                                            </div>
                                            <div class="card-footer ">
                                                <div class="entry-date"> <?php echo time_ago(); ?></div>
                                            </div>
                                        </div>
                                    </a>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php
                    endif;
                    ?>
                </div>
            <?php endforeach; ?>
            <!--        end list category -->
        </div>
        <div class="col-lg-4 col-md-4 h-100">
            <?php get_sidebar(); ?>
        </div>
</main><!-- #main -->

Share this Article !


You may like this
Top 10 most popular programming language in 2025

Top 10 most popular programming language in 2025

Nowadays, we know and see coding skills hold immense value in technology beyond 1990 till 2025, with effects on career advancement being profound. Extremely popular languages such as Python, JavaScript, and Java...
how to improve seo and increase audience for your website

how to improve seo and increase audience for your website

To increase your audience and boost your SEO (Search Engine Optimization), you’ll need to implement a mix of technical strategies, content creation, and marketing campaigns. This is a step-by-step guide to help...
Moodle – how get total course completed and login chart in current year into dashboard page

Moodle – how get total course completed and login chart in current year into dashboard page

To add the total course completed and login chart in the current year to the dashboard, you need to get a query from course complete and login from the database. We created...
(100% worked) How to fix PHP is not recognized as an internal or external command for Xampp Or Wamp

(100% worked) How to fix PHP is not recognized as an internal or external command for Xampp Or Wamp

5 steps for fixing PHP is not recognized as an internal or external command for Xampp rr Wamp. You can follow the tips below. Go to My Computer->properties -> Advanced system setting->...
boostrap columns left in desktop and right in mobile

boostrap columns left in desktop and right in mobile

When you want to change columns left on desktop and right on mobile, we always think about the responsive screen.This tip will help you. following the below code : <div class="container">  ...
Boost your revenue potential with the new ad intents format for Auto ads

Boost your revenue potential with the new ad intents format for Auto ads

google adsense released on 16 April 2024 about the new ad intents format for Auto ads,google adsense’re launching a new format for Auto ads called ‘ad intents’. Ad intents is an intent-driven...