Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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 ); ?>>
share|improve this answer
    
This was my original approach but i couldn't get it to echo within a class="<?php...?>" –  James B Sep 4 '12 at 17:16
    
see the update, that should work. –  amit Sep 4 '12 at 17:26
    
Thanks very much for your help! –  James B Sep 4 '12 at 17:35

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' );
share|improve this answer
    
^ nice, im assuming i put this in the functions.php? Also where would i then have the input for this? –  James B Sep 4 '12 at 16:36
    
Yes, that would go in functions.php. What do you mean by where would I then have the input for this? –  Chip Bennett Sep 4 '12 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 '12 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. –  Chip Bennett Sep 4 '12 at 18:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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