Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Tsunamijaan/2f1ebf9df087a495a1b6e2b9627d0b85 to your computer and use it in GitHub Desktop.

Select an option

Save Tsunamijaan/2f1ebf9df087a495a1b6e2b9627d0b85 to your computer and use it in GitHub Desktop.
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..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment