• Hi Everyone

    I am new to wordpress, so i am trying to learn as i go along. I have an issue with posts on the index page that i am hoping someone can give me some advice on.

    Is there a way that i can give every third post a different class? For instance i have three post all floated to the left and i need to give the third item a different class, so that it removes the margin.

    Ok before posting this message, i have had another look at the Codex, and found the Post Classes function. Would this function perform the task that i am looking for?

    Thanks to anyone who can help out.
    Scott

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    Thread Starter scott-james

    (@scott-james)

    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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding a different class to every third post?’ is closed to new replies.