1

essentially I need to be able to add a class to a post when it show in the list (say index.php) so when in the back end you can say oneCol, twoCol, threeCol and it will then output this within the loop post class.

This is to enable a tighter control of the layout. using this line of code for the output?

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>

Thanks in advance!

2 Answers 2

1

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 ); ?>>
2
  • This was my original approach but i couldn't get it to echo within a class="<?php...?>"
    – James B
    Sep 4, 2012 at 17:16
  • see the update, that should work.
    – Amit Kosti
    Sep 4, 2012 at 17:26
2

There's a filter for that.

Example:

function wpse_filter_post_class( $classes ) {
    // How you determine what class is up to you;
    // We will assume you've determined the class name
    // and added it to $my_post_class
    $my_post_class = 'some-class';

    // Add it to the array of post classes
    $classes[] = $my_post_class;

    // Return the array
    return $classes;
}
add_filter( 'post_class', 'wpse_filter_post_class' );

Edit

Assuming you'll be using custom post meta data to determine the layout string to add as a post class, you'd do something like follows:

$my_post_class = ( isset( get_post_meta( get_the_ID(), '_post_layout', true ) ? get_post_meta( get_the_ID(), '_post_layout', true ) : false );

To incorporate that into your filter callback:

function wpse_filter_post_class( $classes ) {

    // Test for your layout post meta
    $my_post_class = ( isset( get_post_meta( get_the_ID(), '_post_layout', true ) ? get_post_meta( get_the_ID(), '_post_layout', true ) : false );

    // If it's there, use it
    if ( $my_post_class ) {
        // Add layout as a post class
        $classes[] = $my_post_class;
    }

    // Return the array
    return $classes;
}
add_filter( 'post_class', 'wpse_filter_post_class' );
4
  • ^ nice, im assuming i put this in the functions.php? Also where would i then have the input for this?
    – James B
    Sep 4, 2012 at 16:36
  • Yes, that would go in functions.php. What do you mean by where would I then have the input for this? Sep 4, 2012 at 16:50
  • on the back end as i would be editing or creating the post i would need to et the post class to "oneCol, twoCol, threeCol" etc.
    – James B
    Sep 4, 2012 at 16:55
  • You're talking about adding custom post meta data? If so, that would be a separate question, but I'll add a basic implementation. Sep 4, 2012 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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