Posted by Fadi Hania on 30th July 2010

WordPress: Categorize Posts in Pages

This article describes how to categorize posts in pages by modifying your theme template to show posts belonging to a specific category, and to exclude these posts from your home page.

When using WordPress to build your website and blog you need a lot of customization before being able to start blogging. One of the customizations that I needed for my website is how to add separate pages to show posts based on category or tag. These posts also will be excluded from the latest posts list in your home page.

Adding new pages

Before you start add new page for your posts and backup your theme template files that you can find at: wp-content/themes/your-theme-name/.

The next step is to create two new php pages in your theme folder: index-content.php and page-content.php. After creating both pages open the index.php page and copy its content to the new index-content.php excluding the header and footer function calls:

<?php get_header(); ?>
    /*The code to copy*/
    /*End of code to copy*/
<?php get_footer(); ?>

The contents of index-content.php page should look like this:

<?php if (have_posts ()) : ?>
    <?php while (have_posts ()) : the_post(); ?>
<?php endif; ?>

Repeat the same step for page.php and page-content.php pages.

Excluding posts from home page

The next step is to exclude these posts from your home page. I used for that the query_posts($query) function which should be called before the start of the loop. This function can be used to control which posts show up in The Loop and also how they show up. It filters posts based on category, tag, author or other parameters (according to WordPress documentation).
You can show the posts belonging to ‘WordPress’ category:


You can also show the posts belonging to category id 20 and exclude id 3:


Now replace the copied content in index.php with the following code after replacing [your category] with the category name to exclude from the main page:

<?php get_header(); ?>
    $page_cat_id = get_cat_id('[your category]');
    $query = 'cat=-' . $page_cat_id;

    <?php include 'index-content.php'; ?>

    <?php wp_reset_query(); ?>
<?php get_footer(); ?>

The above code first gets your category id from name, then it builds the query to exclude that id and pass it to query_posts() function. After that it includes your index-content.php page which contains the loop to show up the posts.

Showing excluded posts in your new page

The next step is to replace the copied content in page.php with the following code after replacing [your category] with the category name to show on your new categorized-posts page and [your page] with that page name:

<?php get_header(); ?>
    global $post;
    if ( is_page('[your page]') ) : ?>
        <!--Optionally display the page title-->
        <?php query_posts('category_name=[your category]'); ?>

        <?php include 'index-content.php'; ?>

        <?php wp_reset_query(); ?>
    else :
        include 'page-content.php';
    endif; ?>
<?php get_footer(); ?>

This code checks if the requested page is your new categorized-posts page, then it calls query_posts() function to show your category posts in the loop. In order to show up the posts it includes your index-content.php page. If the requested page is not your new categorized-posts page it displays the normal page content by including your page-content.php page.


This tutorial concentrates on how to create additional page to show your posts based on the category, but you can use it to create a separate page for a specific author, for tags or any other parameters you need.

If you have any questions, please leave a comment.