Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add a custom post class in WordPress
<?php // First, create a custom field named 'custom_class' in WordPress and give it a value ?>
<?php // The Loop...
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php // Declare a variable using the custom field
$custom_classes = get_post_meta($post->ID, 'custom_class', false); ?>
<?php // Use the variable in the post class ?>
<article <?php post_class($custom_classes) ?> id="post-<?php the_ID(); ?>">
Copy link

freshyill commented Aug 17, 2011

This is the first time I've created a gist, so I hope I didn't do anything stupid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment