How to offset posts in one category by 3 in main page's loop? (4 posts)

  1. William
    Posted 5 years ago #

    I have a "featured" section on the main page which highlights selected blog posts, and this section sits right above the main blog loop. How do I edit the following code to exclude the three latest posts in category id 7 (category name is "Featured") from displaying in the main blog loop?

    The code below is from my theme's index page; I have a hunch this is where the change would need to be made, but my searches haven't turned up anything that deals with this specific bit of code.

    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>

    If I'm wrong, let me know! Thank you in advance for your help!

  2. Michael
    Forum Moderator
    Posted 5 years ago #

    not directly what you are asking for, but a possible way to achieve what you want;
    check the methods for 'do-not-duplicate' in this docu:

  3. William
    Posted 5 years ago #

    Hmm... Thanks alchymyth. To be honest, this doesn't look very familiar to me. Would this really achieve what I'm looking for, but in a round-about way?

    I'm open to any other suggestions. I've got a feeling the solution is probably a simple one (slight modification of existing code?).

  4. Mark / t31os

    Posted 5 years ago #

    Inside your featured posts loop store the IDs of each post inside an array, then in your the main loop use that array to exclude those posts from the main query..

    Before your featured posts loop..

    <?php $featured_ids = array(); ?>

    Inside the featured loop..

    <?php $featured_ids[] = get_the_ID(); ?>

    Before your main query/loop(what you posted above)..

    <?php query_posts( array_merge( array( 'post__not_in' => $featured_ids ), $wp_query->query ) ); ?>

    I don't think you'll make it any more simple than that... so i hope that helps..

Topic Closed

This topic has been closed to new replies.

About this Topic