Most of my posts have a design like this post: a picture near the top, some text, both at a fixed width column against a white backdrop, same typography and colour scheme throughout. For all the convenience WordPress offers to organize and publish web content, the cost is a certain degree of monotony in presentation. Most of the time this trade off is acceptable. But sometimes you want to accommodate an oversize picture, or write a caption in the left margin, or use a different coloured canvas. Sometimes you want a blog entry to have its own style. To that end, I created the Weekend Food Blogging series to let me experiment with alternative layouts, colour, typography and, (why not) recipes. Recipes already have a great deal of inherent structure — a list of ingredients, preparation instructions, possible variations — and so leave you room to focus fully on their presentation. Plus, if the custom design doesn’t pan out, then there’s good food to be had.
This approach is not for the timid. But if you get as much satisfaction out of designing at the post level as I do, then there are ways to minimize the tedious bits through stylesheets, custom fields and user-defined functions file. The rest of this post explains just how to do that.
- First, create the unique blog post styles.
So far, I’ve kept navigational elements such as tags, categories and menus consistent among the design variations so that visitors would not have to re-learn the interface. For the <a hget_permalinkosturl id=662]” title=“Chocolate Chip Cookies”>Chocolate Chip cookie recipe I changed the title, the excerpt, the links and added a big cookie to the background. Here is a snippet of the CSS that shows the background cookie:body.postid-662 { background-color:transparent; background-image:url('chocolate-chip-cookie.png'); background-position:right 45px; background-repeat:no-repeat }
I use a Thematic, a WordPress theme framework that adds unique post and body classes you can use to easily modify any post through CSS. If you do not, you will likely have to make use of !important declarations to control the presentation.
- Associate the new styles to the blog post.
You can simply add these styles to the existing stylesheet. However, that approach for each custom design is a little tedious compared to the alternative that makes use of WordPress custom fields. Custom fields let you set up just about any design variation within the administration panel that you can turn on with a name/value switch. Here I’ve specifiedcustom_cssas thenameand added the corresponding styles as thevalue. The custom fieldname/valuepair looks like this:

The styles are now part of the post’s metadata and are available to use in WordPress theme files. In this case you will retrieve the custom styles in theheader.php. - Link to the styles in the header.
While I could have added this function directly to theheader.php, I’ve chosen to define a separate function calledcustom_stylesheet_headand add it to thefunctions.php:<?php // Add custom stylesheet to head function custom_stylesheet_head() { if (is_single()) { $css = get_post_meta($post->ID, 'custom_css', true); if (!empty($css)) { echo '<style type="text/css">'.$css.'</style>'; } } } add_action('wp_head', 'custom_stylesheet_head'); ?>
First, the code makes sure we’re on an actual post’s page by using the WordPress conditional tag
is_single(). Then it looks for a custom fieldcustom_css. If one is found, its value is displayed between<style>and</style>tags.
Theadd_actionfunction with thewp_headhook, to call the custom_css function. This tells WordPress to run the function whenever thewp_headfunction is called by theheader.php). This approach leaves my theme files in tact, while I tweak without risk of overwriting the file when I change or upgrade my theme, and without risk of breaking my theme.
The reward for all this effort is the relative convenience of custom designing your next blog post. When you write a post that will receive custom CSS styling, create the custom field named custom_css and paste your custom CSS styles as a value.
3 Comments
Was there a part 2 and more? This is really cool, I’d love to read more if you have to write more! ;)
Part 1 does imply at least a part 2, doesn’t it? I have had another part in draft for some time, but I’ve been hesitant to publish it because the CSS is invalid. Thanks for reminding me…I’ll look at that post again and see if another solution occurs to me.
Just what i was looking for, not much infomation about custom blog post about. Will come in handy in a few weeks.
Lee
5 Trackbacks
[…] Customizing Individual Blog Posts, Part 1 ? ?? ???? ????? GPS MapCard ?????? ???? ??? ?? ????? ?? ??? 5?? ??Paul Mac Tips #1 : ??? ? ?? ??? ???????? ?? […]
Helpful tutorial! RT @adellecharles: Customizing Individual Blog Posts [link to post] #WordPress (via @pimpmywordpress)
[…] This post was Twitted by tjsitback — Real-url.org […]
RT @adellecharles: Customizing Individual Blog Posts [link to post] #WordPress (via @pimpmywordpress)
— Posted using Chat Catcher
Twitter Comment
Customizing Individual Blog Posts [link to post] #WordPress (via @pimpmywordpress)
— Posted using Chat Catcher