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.

  1. To display related posts on a page.
  2. 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
  3. 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.
	}
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter

{ 7 comments }

A few posts to share from the year 2009 and 2008

December 26, 2009

I would like to share a few posts which I think are useful.

Tip : Do more with Format Painter
You can be a victim of phishing by blindly following a link in an email
Access dictionary quickly from Apple applications on Mac
Obscene comments on your blog can put you behind the bars
Use Computer in Eco-friendly manner
Useful keyboard [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

Merry Christmas and Happy New Year ‘09

December 25, 2009

Wishing all of you a very Happy New Year and Merry Christmas ‘09.
This year was full of changes and very busy for me. I was too occupied with many projects. I couldn’t even blog after June 3rd.
I will try to blog regularly in the year 2010. I am also planning to include more topics.
Once again [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

Change photo capture date and time : iPhoto

June 3, 2009

Clicked pictures with incorrect date and time? It become really hard to organize photos with wrong date and time.
Most of the picture editing software doesn’t allow to change photo capture date but iPhoto gives a convenient way to update this information.
Change date and time of only one picture
Select the picture and change the date & [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

Online friends list in Orkut

April 16, 2009
Orkut: Gmail online friends list

Orkut is well integrated with gmail chat. Now you may also notice “Online friends” tab in the left-bottom corner of orkut.
Click on the tab to expend and see the online gmail contacts.

Share and Enjoy:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

1 US dollar is equal to INR 1.419

April 6, 2009

Surprised!!!
According to Unit Convert widget in Mac OS X’s dashboard US$1 is equal to INR 1.419.
And one British Pound is equal to INR 22.649.
That is not all, INR 1 is equal to INR 0.222.
This isn’t only on one Mac. I have tried it on three other Mac’s on different locations in Delhi, India and the [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

Gmail Labs : Undo send

March 22, 2009
Thumbnail image for Gmail Labs : Undo send

Gmail adds a new labs feature to undo send mail. This feature can retrieve the sent message.
Gmail will hold the message for 5 seconds after hitting send button. This feature is quite useful when you press send button accidently before finishing your message or forget to attach file. You don’t have write a “Oops” message [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

iPod Shuffle 3rd Generation – limited functionality without it’s headphone

March 13, 2009

Apple recently launched iPod Shuffle with a new feature VoiceOver. It also has a big change, no control buttons except the Shuffle, Play in order and Off. The functionality is quite limited if not use with the default headphone. When you switch on the shuffle with anything plugged into headphone jack it starts playing but [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →

Tip : Do more with Format Painter

March 10, 2009

Format Painter is a very good tool in MS office to copy and paste style. If you click only once on the Format Painter icon to copy the style, the Format Painter goes away after one application.
Double click on the format painter icon and you can use it on different part of the the document [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • Technorati
  • Twitter
Read the full article →