1

I am using 2 loops on the main page, one that shows the last post from the featured category and a general one with all the posts. The problem is that i get the last post in the featured category in both loops on the same page. It basically duplicates the post on that page. I can only seem to find how to exclude the entire category and I know how to do that. That is not what i need. I only want the latest post from the featured category excluded.

I was thinking i could somehow pull the ID of the post from the featured categ that I am already showing, store it in a variable and then exclude that variable from the main loop. However I am a beginner with both wordpress and php and am not quite sure how to do this.

Any thoughts ?

edit: this is the code i want to change:

            <?php
            $myPosts = new WP_Query( 'category_name=Featured&posts_per_page=1' );while (     $myPosts->have_posts() )
            : $myPosts->the_post();
            ?>
                <?php 
                if (has_post_thumbnail( $post->ID ) ):
                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
                endif;
                $image_url = $image[0];
                ?>
                <div class="index-featured-post" 
                style="background:linear-gradient(to bottom, rgba(26,26,26,0.8) 0%, rgba(110,110,110,0.5) 50%, rgba(26,26,26,0.8) 100%), url(<?php echo $image_url; ?>)">
                    <div class="row">
                        <div class="large-8 large-centered columns">
                            <span class="cat-index"><?php the_category(); ?></span>
                            <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>
                            <span class="post-tag-line"><?php the_field('post_tag_line'); ?></span>
                            <p class="ert-featured"><?php acn_estimated_reading_time() ?></p>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?> 


            <?php if ( have_posts() ) : ?>

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

                    <?php
                        get_template_part( 'content', get_post_format() );
                    ?>

                <?php endwhile; ?>
           <?php endif; ?>
1
  • Please add the relevant code snippet.
    – birgire
    Sep 15, 2014 at 13:49

1 Answer 1

0

If you store your last post ID in a variable from the first loop you can use post__not_in in the second loop to exclude the Id.

Example adapted from Wordpres Class Reference/WP Query

$query = new WP_Query(
    array(
        'post_type' => 'post',
        'post__not_in' => array( $excluded_post_id )
    )
);

You can exclude any number of posts by padding their ID's in post__not_in. Just make sure you get the ID for the post you want to exclude in it's loop or you'll likely end up trying to exclude the current page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.