As milo stated, I need to use the post_class
function to use the post_class
filter.
By adding <?php post_class(); ?>
to the div in my loop I was then able to successfully modify the post class. Below is my full code containing the post_class
filter and the post_class
function.
Reference: https://codex.wordpress.org/Function_Reference/post_class
<?php
//Call in the header
get_header();
//Add Post Class Filter
add_filter('post_class', 'sk_post_class');
function sk_post_class($classes) {
global $loop_counter;
$classes[] = 'one-third';
if (($loop_counter + 2) % 2 == 0) {
$classes[] .= 'first';
}
$loop_counter++;
return $classes;
}
$args = array (
'post_type' => array( 'test' ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<div id="post-<?php the_ID(); ?>"<?php post_class(); ?>>
<a href="https://wordpress.stackexchange.com/questions/231964/<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</div>
<?php }
} else {
echo 'No posts found';
}
// Restore original Post Data
wp_reset_postdata();
// Call in footer
get_footer();
?>