Created
January 5, 2019 08:55
-
-
Save Tsunamijaan/2f1ebf9df087a495a1b6e2b9627d0b85 to your computer and use it in GitHub Desktop.
How to loop posts from a specific category in homepage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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