Inside a template file, the standard loop syntax will display only the information about that page( In fact, a template file is no more than a page ). If you want to display some posts you need to run a custom query or to include ( get_template_part()
) another template file which normally runs a query for some specific post types or categories.
Page template example
/*Template name: Example page */
get_header();
/* The following loop refers to the page object */
if( have_posts() ) : while( have_posts() ) : the_post();
the_title();
the_excerpt();
endwhile; endif;
/* Now you can query for your post using WP_Query Class */
/* Defining $args array of arguments */
$args = array(
'post_type' => 'post', // maybe page | custom post type | an array of these
'posts_per_page' => 6, // number of items you want to display
);
/* Define new $query object to loop through */
$query = new WP_Query( $args );
//Start your custom loop
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
the_title();
the_excerpt();
endwhile; else:
echo 'No posts found';
endif;
get_footer();
Let me know if you get stuck : )
Please, look these from WordPress Codex:
UPDATE
If you look at my example code you will see an $args array. You can define several options within it please refer to WP_Query docs.
Note: have_posts()
; and the_post();
functions don’t accept any arguments.
If you want to use the in_category();
you have to do it in the loop.
Custom loop
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
if( in_category( 6 ) ){
//do something
}else{
//do something else
}
endwhile; endif;