Wordpress has different options for organizing posts. One of them is Categories. The categories can only be assigned to posts, not to pages in Wordpress. But sometime you might want to categorize the pages as well. Here are a few cases.
- To display related posts on a page.
- To display pages and categories in navigation menu in mixed order. Wordpress provides the two different functions to display a list of Pages and Categories. But it doesn’t give the option to setup a navigation menu mixed with pages and categories, like the example given below. In such a case you can map pages to categories and use them as category pages. Obviously it will need some changes in the template.
- Page 1
- Page 2
- Category 1
- Page 3
- Category 2
- To display pages on the category pages with the posts.
How to map Categories to pages
Add following lines of code in the function.php of your theme directory. If you don’t have a function.php in the theme directory then you can add it using any text editor. You can also download the plugin which I have made from http://wordpress.org/extend/plugins/map-categories-to-pages/.
function add_category_box_on_page(){
//add meta box
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'low');
}
add_action('admin_menu', 'add_category_box_on_page');
It will show the category selection box on the right side on “Edit Page” page.
Template changes
You will also have to edit the page template to use categories. You can use standard category code on your page template. Take a look at http://codex.wordpress.org/Function_Reference/get_the_category and http://codex.wordpress.org/Template_Tags/the_category.
Here is a small piece of code to build a page of posts. This example uses the styles from Wordpress default theme.
<?php
global $post;
$categories = get_the_category($post->ID);
$showposts = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => $categories,
'showposts' => $showposts,
'caller_get_posts' => $do_not_show_stickies
);
$my_query = new WP_Query($args);
?>
<?php if( $my_query->have_posts() ) : ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
//necessary to show the tags
global $wp_query;
$wp_query->in_the_loop = true;
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '
'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
Displaying pages on Category pages
When Wordpress fetches the posts from database, the default post type is ‘post’. So even when you have mapped a page to a category, it won’t display the page on that category page. The post type needs to be changed explicitly on the category page. To change the post type, add following code in function.php.
//call a function just before the query runs to fetch posts
add_action('pre_get_posts','change_post_type');
function change_post_type($var) {
if(is_category()) {
$var->query_vars['post_type'] = 'any';
//it will change the value to 'any' from the default value of ;post';
//can be any, attachment, page, post, or revision.
//'any' retrieves any type except revisions.
}
}
{ 7 comments }











