Hi My Name is Jim Krayer
  • RSS Feed

Template all Posts in a Wordpress Category Automatically

The reason to use Wordpess to manage a web site for one’s self or for a client is to make as many aspects of publishing content to the web as simple and automatic as possible. The Wordpress post is the obvious answer for all things automatic but on its own it can be a pretty homogeneous solution.

enter custom fields

At first glance custom fields may seem difficult to utilize or of dubious value but they are incredibly valuable.

Break down what information each of your posts will contain and ask yourself, “Is there any part of this post I might want to use elsewhere without having to show the entire post?” If the answer is yes then use a custom field for each piece of information you’ve answered yes about.

You should also ask yourself, “Will posts of one type contain unique elements that posts of another type will not?” If the answer is yes then you may also need to utilize a custom field.

A great use for a custom field is a testimonial. When a visitor is looking at a product or service they can see the testimonial and with a little clever coding you could call that testimonial elsewhere on the web site without having to use the entire post.

template posts automatically

So far you’ve divided your posts into different categories that have different content and utilize different custom fields and therefore need different code to display them properly. For my sake I have two basic post types; portfolio posts made up of the print portfolio and the web portfolio and blog posts for everything else. I control their display in single.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< ?php
 
	if (have_posts()) {  
 
		// if in the print or web portfolio cats do this
		if (in_category('3') || in_category('4')) { 
 
		// start loop for portfolio cats
		while (have_posts()) {  the_post(); 
 
DO STUFF
 
		// end portfolio while loop
		} 
 
		// end if portfolio. else other posts
		} else { 
 
		// start loop for other posts
		while (have_posts()) {  the_post(); 
 
DO STUFF	
 
		// end other posts while loop
		} 
 
		// end else other posts
		}	
 
	// end if have_posts
	}
?>

benefits of automatic templating

The clear benefit of this method is that there are no additional steps added to the process of writing posts and publishing content. Fewer steps for publishing content, rather than more steps, will always make you and your clients happy.

On a site like this where my portfolio needs to always contain my newest and best work, queries on the category pages limit the results to the newest few entries. Social book marking, comments and ping backs that are vital to my blog posts do not interfere with my portfolio posts.

one method or best method?

In a situation where my posts are either of the portfolio kind or of the blog kind I think this method is fine but it’s not my solution for all sites that need to have multiple formats for posts. There are some good plugins at wordpress.org that address post templates.

Furthermore this method can quickly bloat your single.php file. So if you find you have more than three categories of posts that require a unique template I suggest creating a template file for each category post and using single.php to load the correct template. This method is explained by Matt at mattmedia.net.

This article was posted in wordpress.

  • Digg
  • Reddit
  • StumbleUpon
  • Slashdot
  • Google Bookmarks
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • MySpace
  • PDF
  • email

leave a reply (fields in red are required)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

You can follow any responses to this entry through the RSS 2.0 feed. Or leave trackback from your own site.

Available for Freelance Projects
Silhouettes 11.12.09

Silhouettes 11.12.09