Rather than creating pages and assigning a template, I would use the category_template
filter to load a specific template for all those particular categories. In this example I use a hardcoded array, but this could be adapted to load an option which stores the category slugs you want the template applied to.
function wpa_category_template( $templates="" ){
$special_categories = array(
'one',
'another',
'more'
);
$this_category = get_queried_object();
if( in_array( $this_category->slug, $special_categories ) ){
$templates = locate_template( array( 'special_category.php', $templates ), false );
}
return $templates;
}
add_filter( 'category_template', 'wpa_category_template' );
You then no longer have to query for these posts in the template, as the posts are already in the main query. (also, as an aside, never use query_posts
).
Within the template you can use single_cat_title
to output the name.
You also don’t have to use two queries and loops to style the first post differently, just check the current_post
var in the loop to know what post you’re currently outputting.
if (have_posts()):
while (have_posts()):
the_post();
if( 0 == $wp_query->current_post ):
echo 'this is the first post';
else:
echo 'this is post > 1';
endif;
endwhile;
endif;