In WordPress, you can definitely tack on extra classes to the post_class()
function right in your template files. It’s super handy for styling your posts differently or adding some JavaScript functionality later on.
You just need to pass the additional classes as an argument to the post_class()
function. You can do it like this:
post_class( 'my-extra-class' );
If you’ve got more than one class to add, just put them in an array:
post_class( array( 'my-extra-class', 'another-class' ) );
This would output something like class="post my-extra-class another-class"
on the post’s HTML element, along with the default classes that WordPress includes.
If you’re in the loop and want to conditionally add a class based on, let’s say, post meta, you could do it like this:
$classes = array();
if ( get_post_meta( get_the_ID(), 'my_meta_key', true ) ) {
$classes[] = 'class-based-on-meta';
}
post_class( $classes );
Just drop that into your template where your loop is, and you’re the cats pajamas!
