The following code should add the class “third” to every 3rd post:
<?php if (have_posts()) : ?>
<>?php $c = 0;while (have_posts()) : the_post(); $c++;
if( $c == 3) {
$style = 'third";
$c = 0;
}
else $style='';
?>
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">
Not sure what you looked at (I couldn’t find the Post Classes function).
But I did something similar on my theme. Use this in The Loop inside the php file(s) in question (most likely home, index, category, and archive) and it should work.
<?php if (have_posts()) : ?>
<!-- new variable for post count -->
<?php $i = 1; ?>
<!-- begin THE LOOP -->
<?php while (have_posts()) : the_post(); ?>
<!-- increments post count with each post -->
<?php $i++; ?>
<div class="<?php the_ID(); ?><?php if ($i == 3 || $i == 6 || $i == 9 || $i == 12 ) { ?> thirdpost_class<?php } ?>">
<h2 class="archive-header"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title() ?></a></h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title();?>
</a>
<?php the_excerpt(); ?>
<p class="postmetadata"><?php the_time('M d, Y') ?></p>
</div>
<!-- end THE LOOP -->
<?php endwhile; ?>
<?php endif; ?>
Also, see The Loop in Action for more detail on how to customize The Loop.
Thanks so much for taking the time to reply to this thread esmi and mrengy.
I have tried both pieces of code, and i can’t get either of them to work. The new class doesn’t attach to the third post, and when i view the source code it isn’t present any where. Have you got any idea of what could be causing the issue?
Thanks once again
It might help if you dropped a copy of the file into the WordPress pastebin and post the pastebin url here.
esmi some of your code had errors, correction below.
<?php if (have_posts()) : ?>
<?php $c = 0;while (have_posts()) : the_post(); $c++;
if( $c == 3) {
$style = 'third';
$c = 0;
}
else $style='';
?>
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">
Ben
(@benredpath)
seanjacob, thanks thats just what I was looking for, works a treat!