• Hello,

    I’m relatively new to wordpress and php would really appreciate some help on a theme that I’m editing.

    At the moment when you click on a category it displays all the posts with the same class of “posts”.

    what I want to do is make it so that every 5th post has class of “post multiple5”. This way I can style every 5th post with a bottom-border, thus breaking up my posts into blocks of five.

    is there an easy way to do this? if so I would really appreciate it if someone would show me how to do it.

    Thanks in Advance ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just takes a little PHP inside the loop that outputs your posts:

    <?php
    $postcount = 0;
    while (have_posts()) : the_post();
    $postcount++;
    ?>
    
    <div class="post<?php if($postcount % 5 == 0) echo " multiple5"; ?>">

    % is the modulus operator, it returns the remainder of the left number divided by the right number. So when $postcount hits 5 it will return 0 and cause the extra class to be output.

    This code does not work for me. 2.6.1 WordPress version. Gives me an error at the end of the loop.
    Used it this way and it worked:
    <?php
    $postcount = 0;
    if (have_posts()) :
    while (have_posts()) : the_post();
    $postcount++;
    ?>

    theres absolutely nothing wrong with the code he provided, and it works just fine in 2.6.1, and as expected.

    If you are seeing an error than you didnt edit your file(s) properly. What he provided was an example; not a verbatim ‘copy and paste into a theme file’ fix.

    Look at what he provided, look at your theme files, and put on your thinking cap.

    ## I posted while you were doing the above as you’ve since edited your reply.

    Thread Starter prettierpixels

    (@prettierpixels)

    Thanks for the help guys. Sorry it has taken me so long to reply. I had come up with a solution that was nowhere near as elegant, I’m going to replace it with the code above straight away.

    again thanks ??

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Not many people know this, but if you’re using the normal have_posts/the_post loop style, then the internal query keeps a count too. You don’t have to build your own counter.

    Look closely at $wp_query->current_post. It starts at zero. ??

    if ((($wp_query->current_post + 1) % 5) == 0) ...

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Assigining an additional class to ever 5th post’ is closed to new replies.