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…).
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, 882 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.
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.