Sometimes you need to have a page with all the posts in the same category listed on it but by default you can’t really do this in WordPress as there are no categories on posts.
Some solutions include adding a plugin where you define the categories in shortcode, other require several templates in your theme, but the solution I have implemented fits my needs perfectly – I have added categories to the page and then made a query to display only posts that in the same categories. It comes very handy when you want to map multiple categories to one page and when you have many pages.
In functions.php you have to register taxonomy to a custom object type. As a result categories will be added to pages admin.
function map_page_category() { if(function_exists('register_taxonomy_for_object_type')){ register_taxonomy_for_object_type('category', 'page'); } } add_action('init', 'map_page_category');
Then in the theme template you first determine the categories of the current page and then create a query to get the posts in the same categories. The last part is to dipslay the posts on the page as you wish – in this example you have a thumbnail, category, title, excerpt and links to the post:
<?php // first reset query to clean up after any previous custom query wp_reset_query(); // then get and save each category defined in the page in the array e.g. Array ( [0] => 12 [1] => 13 [2] => 2 [3] => 3 [4] => 4 ) $cats = array(); foreach(get_the_category() as $category) { $cats[] = $category->cat_ID; } /* Print_r for debugging - to see which categories were defined */ // print_r($cats); // arguments - edit as you please $args=array( 'post_type' => 'post', /* show only posts and not pages */ 'category__in' => $cats, /* array(12,13,2,3,4) */ 'posts_per_page' => '-1', /* show all posts */ 'orderby' => 'date', /* order by date */ 'order' => 'DESC' /* order descending */ ); $cat_query = new WP_Query($args); ?> <?php if( $cat_query->have_posts() ) : ?> <?php while ($cat_query->have_posts()) : $cat_query->the_post(); ?> <?php // code you wish to display // you can use html + template tags echo '<li class="post">'; /* IF THE POST HAS THE THUMBNAIL */ if (has_post_thumbnail()) { echo '<a href="'. get_permalink() .'">'; the_post_thumbnail( 'full' ); echo '</a>'; } else { /* IF THER IS NO THUMBNAIL GET THE DEFAULT IMAGE */ echo '<a href="'. get_permalink() .'"><img alt="" src="'. get_template_directory_uri() .'/img/sample-image.png" /></a>'; } $category = get_the_category(); if($category[0]){ echo '<a class="post-category" href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } echo '<h3 class="post-title"><a href="'.get_permalink().'">'.get_the_title().'</a></h3>'; echo '<p class="post-excerpt">' . get_the_excerpt() . '</p>'; echo '</li>'; ?> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <?php endif; ?>