• Resolved Publishink

    (@publishink)


    I’m trying to figure out how to add a unique class to the fourth and last page on my blog page.

    I have four posts. The first is displayed full width and in its entirety. The next three are displayed in divs as excerpts, side by side below the first post. The idea is that each is one-third of the width of the full post above.

    In order to get the proportions of these three right, I need to add a class to the last of the three to remove right padding.

    I’d appreciate any suggestions. The layout of the divs is shown roughly below in code if my explanation isn’t clear.

    <div>Post 1: Full width</div>
    <div id="recent">
    <div>Post 2</div><div>Post 3</div><div>Post 4</div>
    </div><!-- #recent -->
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    If this is in a loop try it with this above the loop to get the post count:

    <?php
    global $wp_query;
    $maxposts = $wp_query->post_count;
    ?>

    Check inside the loop if the current post is the last post:

    <?php $class = (($wp_query->current_post +1) == $maxposts) ? ' class="last-post"': ''; ?>
    <div<?php echo $class; ?>><!-- post content --></div>

    Thread Starter Publishink

    (@publishink)

    Thanks for the response.

    A PHP newbie, I’m afraid, so putting your suggested code in the right place is a bit of a challenge! I know what you mean by ‘before the loop’ at least. But I have two loops. Loop one picks up the first post and loop two the other three posts. I’ve defined a max of four blog posts under ‘Reading’ in the admin area.

    Here’s some of the code from my index.php if you have time to suggest where I can add you code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with a query on the second loop: http://pastebin.com/Sj05UJxN

    Thread Starter Publishink

    (@publishink)

    Brilliant, Keesie(?).

    Took what seemed like an age to get it working, mainly because I did partial cut and pastes and missed the $ class you had added in < article >.

    Many thanks. Consider it case closed and time for me to find a decent resource to learn more WordPress PHP.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Glad you got it resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dynamically add a class to a particular post’ is closed to new replies.