Here is what I tried and couldn't reach it. Maybe I need a whole different approach?

1) I created a custom field in a post, called post-class and inserted the value cita-flow800000 (weird class name means: citations flow structure using the #800000 as the main color, for those who might be curious)

2) I called the new custom_value in my content-single.php in front of the post_class function, and inserted the variable inside the post_class function, like this:

<article id="post-<?php the_ID(); ?>" <?php post_class('class-1 class-2' . $custom_variable); ?>> <?php $custom_values = get_post_meta($post->ID, 'post_class'); ?>

3) I selected the class in my style.css:

.cita-flow800000 p { font-size: 30px; }

As an example, but nothing happens.

Could it be done, to style posts differently and accordingly to post classes? Creating different classes for different post structures and selecting/creating them in the post meta custom fields and in the style.css sheet?

PS - I tried changing the variable inside the post_class function into $custom_values, thinking that $custom_variable could be a typo from the post that shared the recipe, but it still didn't work. All functions seem to be calling all variables correctly, so why doesn't it work?

Anyone who can point me in the direction where I could learn and apply this?

  • Can you check what classes are there in the article? Is cita-flow800000 included in the class? – Nilambar Mar 30 '15 at 3:43
  • I'm searching on how to verify if the article includes the class. No luck. Do you know how? – Rodrigo Cardoso Mar 30 '15 at 21:20

I believe the primary problem in your code is the $custom_values is set after you try to use it (and you need to use the same variable name). Otherwise, it doesn't have a value when it's in post_class(). In fact, I'm surprised you're not getting an error. That variable needs to get the value before the call to post_class.

<?php $custom_values = get_post_meta($post->ID, 'post_class'); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'class-1 class-2' . $custom_values ); ?>> 

In the above snippet, you also need to account for the fact that get_post_meta() returns an array by default. (This means the above code snippet still probably wouldn't work.)

So instead of all that, the best practice is to use the post_class filter (which would also mean you get this class if the post shows up on an archive page too.

To use the filter, you'd use a snippet like this (untested), probably in your functions.php file:

add_filter( 'post_class', 'wpse182657_post_class', 10, 2 );
function wpse182657_post_class( $classes, $post_id ) {
    // get the meta
    // true assumes you only use one value per this key on any single post
    // if false, you'd have to loop through the array with a foreach loop
    $post_class = get_post_meta( $post_id, 'post_class', true );

    // add $post_class variable to $classes array
    $classes[] = esc_attr( $post_class );

    // run along now, $classes
    return $classes;
  • Great explanation. I'm learning with all the details. I still don't get why it didn't work for me, as well as the other recipes I found, both through editing the content-single.php and through a functions.php filter. Anyway, I found one that worked out. Added it in an answer below. Thanks for your input. – Rodrigo Cardoso Mar 31 '15 at 0:01

It's working, finaly. The link to the source and the code which worked out are below.

https://wordpress.stackexchange.com/a/64124/69825 (Check for the Update #1 edit in the answer) It was through editing the content-single.php rather than through a filter in the functions.php file. I tried 2 or 3 recipes of each. I couldn't understand why they didn't work out.

<?php $my_class = get_post_meta($post->ID, "your_meta_name") ?>
<div id="post-<?php the_ID(); ?>" <?php post_class( $my_class ); ?>>

It creates a variable $my_class and adds it to the post_class function. The value is determined in the custom fields of the post meta data.

The variable is created before it is called in the function, as mrwweb advised in the previous answer here above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.