Keith Curreri

Adding a Class Using Custom Fields in WordPress

You may have seen the Custom Field box in WordPress when you edit a page or a post but you may not know how powerful this feature can be.

Let’s say that you have multiple pages on your website and you would like to be able to style each of them differently. Instead of making separate page templates in the theme folder, you can add a line of code to the existing template and add different classes right in the WordPress editor.

This post will assume that you would like to add a class for styling purposes, but there are many other ways Custom Fields can be used.

Step 1: Adding PHP to the theme template

Very simply, if you would like to add a class variable, just paste the following line of code just like you would any other class:

class ="post->ID; echo get_post_meta($postid, 'class', true);?>"

Step 2: Adding a class in the WordPress editor using Custom Fields

Like the image below illustrates, just type in class for the Custom Field name, and the name of the class in the Custom Field value, then click “Add Custom Field”. The page or post that you created will now add a class name to the template.

Share

No related posts.

7 Responses to “Adding a Class Using Custom Fields in WordPress”

  1. Nope, this one’s beyond me. What’s a class variable?

    [Reply]

    Keith's reply on

    Hmmm, if you don’t understand it maybe I worded it wrong, but I can’t think of a better way to describe it. Essentially, class variable would be a class which can be set within the WordPress editor. I’ll have to think of a better term to explain it.

    [Reply]

    Mitch's reply on

    Here’s the problem; I don’t know what “class” means. That’s why the rest of it doesn’t make sense. What does class refer to?

    [Reply]

    Keith's reply on

    I mean a CSS class. Similar to an id. For example

    .
  2. Ah; see, I didn’t know that until you just told me. I hope your other folks reading this are more savvy. lol

    [Reply]

  3. Lol @ Mitch
    Thanks Keith, i needed this info!

    [Reply]

    Keith's reply on

    Thanks Glen! Glad I could help.

    [Reply]

Leave a Reply