adding a custom css class to post

Note –

I recommend using the hook suggested by @Chip Bennett in another answer

Here’s the modified version of that filter –

function wpse_filter_post_class( $classes ) {
    $my_post_class = get_post_meta($post->ID, "your_meta_name");

    if( $my_post_class != '' )
      $classes[] = $my_post_class;

    return $classes;
}
add_filter( 'post_class', 'wpse_filter_post_class' );

You can setup a Custom fileds for that – See usage, then use get_post_meta() function to fetch it and show where you want

Example –

<?php echo get_post_meta($post->ID, "your_meta_name", true)?>

This will output the class name set in the meta_key.

OR ( Update #1 ) –
Pass the variable into Post Class

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

Leave a Comment