WordPress.org

Forums

Offset posts if a condition is met (3 posts)

  1. blogjunkie
    Member
    Posted 4 years ago #

    I customised my theme for http://www.dubrovnikresaturantkl.com. On the homepage, I have a Latest News & Happenings block. The main item pulls the latest post from the Highlights category and displays it with the post image. To the right of that are latest posts pulled from all categories.

    The problem arises if my latest post is not in the Highlights category. The main item will show X and the latests posts will show X again (where X = the last post in the Highlights category). For my current setup - if I add a post to News, the Mother's Day post will appear as the main item and again in the latest posts.

    I want to offset the latest posts on the right, but only if the last post is something from the Highlights category. If the latest post is not in the Highlights category, then don't offset the posts.

    Here's the code I use to offset at the moment:

    <?php
    $latestpostOffset = new WP_Query();
    $latestpostOffset->query('showposts=3&offset=1');
    if ( $latestpostOffset->have_posts() ) : while ( $latestpostOffset->have_posts() ) : $latestpostOffset->the_post();
    ?>
    <div id="post-<?php the_ID(); ?>" class="post hexcerpts">
        <h3 class="entry-title"><a title="Permanent link to <?php the_title(); ?>" rel="bookmark" href="<?php the_permalink(); ?> "><?php the_title(); ?></a></h3>
        <div class="format_text entry-content">
            <?php the_excerpt(); ?>
        </div>
    </div>
    <?php
    endwhile; endif;
    ?>

    I hope I'm making myself clear - it's confusing even to myself! Appreciate your help, thanks

  2. vtxyzzy
    Member
    Posted 4 years ago #

    It seems to me that you don't really want an offset, but rather to exclude the post shown in the main loop (not shown in your code above). If that is the case, save the ID of the post in the first loop to $main_post_id and use this in the loop from above:

    <?php
    $latestpostOffset = new WP_Query();
    $args = array(
       'posts_per_page' => 3,
    );
    if ($main_post_id) $args['post__not_in'] = array($main_post_id);
    $latestpostOffset->query($args);
  3. blogjunkie
    Member
    Posted 4 years ago #

    Thanks, I'll give that a try!

Topic Closed

This topic has been closed to new replies.

About this Topic