How to loop posts from a specific category in homepage

add_action( 'pre_get_posts', function ( $q ) {

    if( $q->is_home() && $q->is_main_query() ) {

        $q->set( 'cat', CATEGORY_ID );
        $q->set( 'posts_per_page', 3 );

    }

});

Or

 $args = array(
     'cat' => <your category ID>,
     'posts_per_page' => 3
 );
 $the_query = new WP_Query( $args );

 // The Loop
 if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {

        // ## write your code here..
    }
 }
The following two tabs change content below.

Md Jahed

Latest posts by Md Jahed (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *