Using Custom Fields Part Two - Styling Per Post

Now we are going to get into the really exciting things that can be done with custom fields! If you haven’t already taken a look at Part One, give it a quick read to bring yourself up to date with the rest of us. In this post (and the posts to come!) we are going to look at some more advanced techniques involving the use of custom fields.

The end of the last post directed readers to take a look at the advanced section of the guide on custom fields in the Wordpress codex. Give it a quick read if you haven’t. The advanced section introduced us to this function:

get_post_meta($post_id, $key, $single)

.

What does this function allow us to do?

  1. It allows us to call up a custom field from a specific post.
  2. It allows us to call up a specific key value.
  3. And it allows us to control how the resulting data is displayed. (In other words whether the data is displayed as an array or a single value.)

In this lesson we are going to focus on number two from the list above. Using the following simple function to call up a specific key value:

<?php echo get_post_meta($post->ID, ‘your key name here‘,true) ?>

Lets run through how we can use this function to create flexible custom layouts on a per post basis.

Complete the following steps:

  1. In your Wordpress admin go to Presentation: Theme Editor: Select the theme you wish to edit in the drop down menu: then select the Main Index Template.
  2. In the Index Template find the area in the image below that is highlighted (located in the The Loop):
    area.jpg
  3. Now replace “post” with
    <?php echo get_post_meta($post->ID,’post’, true) ?>
  4. We are done editing the Index Template make sure to save it. Next go to a create a new post. While writing a new post go down to custom fields to create a new key and value labeled like in the image below, click the add Custom Field button when complete:
    customarea.jpg
  5. After adding the Custom Field you should see something like this:
    aftercustom.jpg
  6. Now type up a simple post. And publish it. If you look at the source code after publishing you should see something like this for the class of your new post:
    <div class=”default” id=”post-1″>
  7. To style it simply create a new styling attribute in your CSS similar to the following:
    .
    default {background: #114433; font-family: Verdana, Arial, Helvetica, Georgia, Sans-serif;
    font-size: 12px;}
  8. Now when publishing a normal post make sure to add the custom field with the key name “post” and to then type in default. This will be the default style. If you want to change the styling just type in a different class name in the value area. Make sure to do this with your single template as well if you wish.

Simple and elegant I think. The possibilities are endless. The only negative is that this is something you need to do at the start of a new blog or risk the need to create custom fields for every post you have published. There is a way around this issue that I will cover in a future post. Next in the custom fields tutorial series we will look at using custom fields to create a custom image per post. (ie. a category icon)

Follow this Custom Fields tutorial series from the beginning.

What's Next?
Related Posts

[…] a las 7:00 de la mañana me he encontrado con este artículo que nos explica como hacer que cada uno de nuestros posts sea diferente. Para ello usaremos una entrada en el meta del post en donde introduciremos la clase CSS que […]

Nice suggestion Brian. I never thought about using custom fields for styling.

@sp: Thanks. Wait until you see some future goodies I have coming.

That’s a nice way of using the custom fields (Something which was a mysterie to me for a long time). But you could also create different styling from the loop. Just with some if statements and the in_categorie() template tag. But this might mess up your code so i’m not sure if this is a good solution.

Nice post SP.
“The only negative is that this is something you need to do at the start of a new blog or risk the need to create custom fields for every post you have published.”

There would be a solution to this I think :

——————–
Index template :
——————–
After “post”, ADD the following :
ID,’post’, true);
echo ($var);
?>

You get :
ID,’post’, true);
echo ($var);?>” id=”post-”>

——————–
Custom Fields Examples :
——————–

Key : post
Values :
default
news

——————–
CSS
——————–

.postdefault {background: #114433; font-family: Verdana, Arial, Helvetica, Georgia, Sans-serif;
font-size: 12px;}

.postnews {background: #554466; font-family: Verdana, Arial, Helvetica, Georgia, Sans-serif;
font-size: 12px;}
——————–

In that case, you don’t need to create custom fields for every post.

Having troubles posting.

Sorry for multi-posts, brian.
My php code was not well displayed.
You can find the full code here :
http://www.petitcrayon.com/temp/custom_fields_styling.gif
You can delete my previous comments.

@Ange: “In that case, you don’t need to create custom fields for every post.”

Well the point of the use of custom fields in this case is to set it up to be useable on a per post basis. But as I said its not necessary to enter a value for each post and there is a way around it that I will cover. Thanks for the function.

@Jaap: I did do a brief look at styling by category. This however can create problems with multiple categories classified for the post. I think styling using the custom fields is the most elegant solution.

@Ange - Brian wrote the tutorial, not me :).

@Brian and Ange - There’s a really simple solution - multiple selectors or classes. Instead of replacing the post selector or giving it a suffix like Ange did, simply place the custom field value after the post selector within

class=”post”

. For example:

class=”post alt”

Oh :) I didn’t know about that.
Thank you, it’s easy to use and flexible.

Thats one way SP. Good point.

I didn’t know that, thanks for sharing !

Celebrating redesigns

I can’t seem to put my finger on it, but for some reason I always have this urge to redesign my site, even though it’s only about 2 months old… I guess I feel this urge, since it’s obviously a lot of fun to redesign a site and o…

I still lost out here. Would you instruct it clearly like in this post? http://themasterplan.in/themes/the-morning-after/

Brian, wicked good ideas. I’ve been trying to find out how to do this for hours, and I was able to do what I wanted in about 15 minutes after finding you article. Thanks.

Brian:

Glad it helped you Tim.

asony:

Hi Small Potato,

Please guide me :

Key = Theme Home Page

Value = http://www.wpdesigner.com/

Now, I want to show this key on ” single post ” page, if only it has value.

Means if any theme has not home page, nothing will be displayed.

How can I ?

And I also want to add link at value.

How can I ?

Thanks.

asony

Post your question at the forums. I’ll answer it there. I can’t post codes in comments.

Reply
Comment Policy
  • Theme support questions should be posted at the support forums.
  • Name and Email are required. Email is never published.
  • You grant this site perpetual license to reproduce your words and name/website in attribution.
  • Inappropriate comments will be removed at my discretion.
Close
Powered by ShareThis