• Resolved pahroblem

    (@pahroblem)


    Hello everyone,

    Was hoping someone could guide me on how to exclude posts in a loop that appeared in another loop on the same page.. here is an example of our front page.

    http://prntscr.com/bqoeuz

    As you can see the first area is the latest articles from throughout the website and the 2nd area is just the latest news..

    here is the code..

    <div id="featured-topics">
    <?php $count = 0; ?>
    <?php
        $args = array(
            'posts_per_page' => 4,
        );
        $posts = get_posts( $args ); ?>
    
    <?php foreach (array_chunk($posts, 1, true) as $posts) : $count++; ?>
    
            <?php foreach( $posts as $post ) : setup_postdata($post); ?>
    
             <div class="column-<?php echo $count; ?>">
    
                      <a class="featured-<?php echo $count; ?>" href="<?php the_permalink(); ?>" style="background-image: url('<?php the_post_thumbnail_url( $size ); ?>' )">
    
                <div class="featured-box">
    
                   <h2 class="featuredheading-<?php echo $count; ?>"> <?php the_title(); ?> </h2>
                   <p class="featured-date">By <b><?php the_author(); ?></b> | <?php the_date(); ?></p>
                   <p class="date-underline"></p>
    
                </div> <!-- featured-box -->
    
                      </a> 
    
             </div> <!-- column/featured -->
    
            <?php endforeach; ?>
    
    <?php endforeach; ?>
    
    </div> <!-- Featured Topics -->
    
    <div id="row-container">
    
    <h2 class="main-heading">Latest game news</h2>
    
    <div class="article-loop">
    
    <?php
    $args = array(
    'post_type' => array( 'post'),
    'posts_per_page' => 4,
    'category_name' => 'news',
    );
    $the_query = new WP_Query( $args );
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    <?php $count = 0; ?> 
    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php $count++; ?>
    <?php if ($count == 4) : ?>
    <div class="featured-article">
    
    <a href="<?php echo the_permalink(); ?>">
    
       <div class="featured-thumb">
    
    <?php
    	echo get_the_post_thumbnail($post->ID, 'plant-thumb');
    ?> 
    
    <span class="article-overlay">
    
            <h4 class="article-item-title"><?php the_title(); ?></h4>
            <p class="featured-date">By <b><?php the_author(); ?></b> | <?php the_date(); ?></p>
            <p class="date-underline"></p>
    
    </span> <!-- article-overlay -->
    
       </div> <!-- featured-thumb -->
    
    </a>
    
    </div> <!-- featured-article -->
    
    <div class="ad-holder">
    <div class="item-ad">
    
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- 300 x 250 -->
    <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-7568065840567088"
         data-ad-slot="1234194857"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    
    </div>
    </div>
    
    <?php else : ?>
    
    				<?php get_template_part( 'content-basic' ); ?>
        <?php endif; ?>
        <?php endwhile; ?>
        <?php endif; ?>
    
    </div> <!-- articles-loop -->

    How would i get it so that the latest news loop doesn’t include the posts from the featured articles above it?

    thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • for general ideas on how to avoid duplicate posts in two loops, see https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action (particular the section after ‘Note for Multiple Posts in the First Category‘)

    Thread Starter pahroblem

    (@pahroblem)

    Thanks for your reply.

    I’ve tried fiddling around with it but i cannot get it to stop the posts duplicating.. can you please show me where i should enter the $do_not_duplicate.

    in the first loop, edit this line:

    <?php foreach( $posts as $post ) : setup_postdata($post); ?>

    for example to:

    <?php foreach( $posts as $post ) : setup_postdata($post); $do_not_duplicate[] = $post->ID; ?>

    in the second loop, add this to the $args:

    'post__not_in' => $do_not_duplicate,

    Thread Starter pahroblem

    (@pahroblem)

    Great! thanks alot for your help Michael

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘help with excluding posts from another loop’ is closed to new replies.