Black God

WordPress: Assigning Category to Pages using custom field and listing pages in a category

WordPress 3.0 has been launched. To celebrate this I was making some improvement in my web site.

Before this release, some times back I had a specific requirement. My requirement was classifying the pages in to categories similar to posts; i.e., assigning Category to Pages. But WordPress does not support it natively. I used a plug-in called Map Categories to Pages. Thanks to the author, this plugin worked fine till WordPress 3.0. Once upgraded to 3.0, I noticed that the newly introduced flexible menu system wp_nav_menu broken in Category archive page (This menu is supposed to list all categories, but it did not display any menu item). After a lot of guessing, trial and error I found that deactivating the Map Categories to Pages plug-in it worked. Let it be.

Now a days I have started tinkering the theme code directly than depending on too many plug-ins and widgets. In line with this I thought of getting this functionality without plug-in. The basic idea is to learn and have my own code :-) Here is how I achieved using custom field. I assume that the reader have enough knowledge in editing theme files in self hosted wordpress.

Assigning category to page

Open a new Page or edit your existing page. In Custom Fields section, You can find Enter new link. Click this to enter a keyword and value. Let’s have keyword as page_category and value as projects. Save the page. Similar to this you can assign different values to the custom field page_category in other pages (e.g.: gallery, downloads…).

Creating a custom field and assigning value to it (Click the image to enlarge)

Now you have assigned your own category to all your pages. Now it is time to list the pages with excerpts in a page (It is similar to Category archive page for posts).

Creating a custom template for a category

We are going to make use of Custom Template in WordPress.
Step-1: Copy the archive.php (or archives.php) file in your theme as gallery.php file. Go to the theme editor and select this new file to edit.

Step-2: Include Template Name: Gallery with appropriate name. It is important to show your template in the template selection box while editing a Page.

Step-3: Have a nice title for your page. I have my page title as “Gallery” with h1 tag.

Step-4: Include a query_posts() call as shown below just before while loop of posts.

<?php query_posts(‘post_type=page&meta_key=page_category&meta_value=gallery’); ?>

Here we have given three arguments:
1. post_type=page: this arguments helps to list only pages (not posts)
2. meta_key=page_category: this is to select our custom field
3. meta_value=gallery: this helps to get the pages with page_category custom field of value “gallery”

Step-5: Reset the query at the end of while loop

wp_reset_query(); // Restore global post data

I have given below gallery.php as reference. If you are using this file as template for you, ensure that replace the all CSS theme reference with yours.

  Gallery Page Template (948 bytes, 612 hits)

List page excerpts based on category

Create a new page with title of your Category. Let the page content be empty. The most important one is that you have to select the right page template. Under Page Attributes section, select your new template. Save the page.

Selecting custom template for Category page

This page should list down your selected pages with excerpts. If it is not enough, you can create a custom link to this page in WordPress Admin -> Theme -> Menus section and add it to your menu.

Hope this post may be useful to you. This work has helped me to understand custom fields and templates. And also I have more control over look and feel of my category page for pages. Please let me know your comments and suggestions to improve.

8 comments for “WordPress: Assigning Category to Pages using custom field and listing pages in a category

  1. Willabeewombat
    April 21, 2012 at 1:37 pm

    I use custom fields for meta data (author, description, robots) and to insert CSS and JavaScript into targeted location in a template.

    One of the things I would like to do is to be able to assign a menu, say, ‘Products’ to any pages and posts that belong to the ‘Products’ category. I can do this just for posts but, as you know, I cannot assign the ‘Products’ category to pages so they cannot be filtered for inclusion in my products category query.

    Do you think your solution here would resolve this issue so that I can place a products menu only on pages and posts that have a category of products? We could then do the same for any other section (category) of the site and give them a section specific menu.

  2. Abc
    December 28, 2011 at 1:21 pm

    aaaaaaaaaaaaaaaaaaa

  3. Chris
    December 17, 2010 at 7:07 pm

    Not being able to have more then one parent page was a major issue for a site I had to build. I figured that custom fields would be the answer. I stumbled on to this page and you solved my problem. THANK YOU!

  4. Philip
    October 24, 2010 at 2:56 pm

    While I am sure this works this is way to complicated.

  5. October 9, 2010 at 10:57 pm

    Perhaps I’m just getting greedy…but is there a way to limit the length of the excerpt? I’ve tried a few snippets but they don’t seem to work, perhaps because it’s pulling from a page and not a post?

  6. October 9, 2010 at 10:06 pm

    This was EXACTLY what I was looking to do. Thank you so much for the post!

  7. August 17, 2010 at 10:15 pm

    I have upgraded the plugin Map Categories to Pages. It should now work fine with WP3 though I haven’t tested it with the new nav functionality.

  8. phil
    July 9, 2010 at 12:59 pm

    when this plugin stopped working after WP 3.0 i looked for hours for an alternative. thanks for being part of that search. i did find the sample code below brought my categories in pages back to life w/o the plugin:

    http://wordpress.org/support/topic/388581

Leave a Reply

Your email address will not be published. Required fields are marked *