Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created January 31, 2012 00:33
Show Gist options
  • Save GaryJones/1707835 to your computer and use it in GitHub Desktop.
Save GaryJones/1707835 to your computer and use it in GitHub Desktop.
How to add additional post classes
<?php
add_filter( 'post_class', 'custom_post_classes' );
/**
* Add a class of "top" to the first post, and "odd"/"even" classes as appropriate.
*
* @global int $loop_counter Counts the number of times we do the loop.
*
* @param array $classes Existing array of post classes.
*
* @return array Amended post classes array.
*/
function custom_post_classes($classes) {
global $loop_counter;
if (0 == $loop_counter) // First post
$classes[] = 'top';
if ($loop_counter % 2) // Loop counter is exactly divisible by 2
$classes[] = 'even';
else
$classes[] = 'odd';
return $classes;
}
#content .post { // This isn't a custom class
background-color: #fff;
}
#content .odd {
background-color: #fdfdfd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment