Where is the query of the content loop? avoid duplicate posts

I have this site thelittleman.es wich Im developing with roots and I have one slider:

    <div class="row"> <div id="myCarousel" class="carousel slide" style="max-width: 1054px;">
     <div class="carousel-inner">
      <?php 
       $the_query = new WP_Query(array(
        'category_name' => 'Home Slider', 
        'posts_per_page' => 1 
        )); 
       while ( $the_query->have_posts() ) : 
       $the_query->the_post();
     
       $do_not_duplicate = $post->ID;
    
      ?>
       <div class="item active">
        <?php the_post_thumbnail('large');?>
        <div class="carousel-caption">
         <h4><?php the_title();?></h4>
         <p><?php the_excerpt();?></p>
        </div>
       </div><!-- item active -->
      <?php 
       endwhile; 
       wp_reset_postdata();
      ?>
      <?php 
       $the_query = new WP_Query(array(
        'category_name' => 'Home Slider', 
        'posts_per_page' => 5, 
        'offset' => 1 
        )); 
       while ( $the_query->have_posts() ) : 
       $the_query->the_post();
      ?>
       <div class="item">
        <?php the_post_thumbnail('large');?>
        <div class="carousel-caption">
         <h4><?php the_title();?></h4>
         <p><?php the_excerpt();?></p>
        </div>
       </div><!-- item -->
      <?php 
       endwhile; 
       wp_reset_postdata();
      ?>
     </div><!-- carousel-inner -->
     <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
     <a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
    </div><!-- #myCarousel -->  </div>

wich is included in base.php using “if is not home”. The slider works perfectly.

Right after that, in base.php I have the regular call to content, I edited the content.php to fit my design needs so its col-md-6 each post.

Well, this works ok but I want to avoid the posts of that category (home slider) or avoid them to repeat. I can do that excluding a category from the main loop (I don´t know how to do that) or applying this trick: How to Avoid Duplicate Post Display with Multiple Loops in WordPress

But when I go to content.php this is how it is:

    <article class="col-md-6" <?php post_class(); ?>>
      <header>
        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php get_template_part('templates/entry-meta'); ?>
      </header>
      <div class="entry-summary">
        <?php the_excerpt(); ?>
      </div>
    </article>

So… where is the initial query for that? where is <?php if (have_posts()) : while (have_posts())??

Also you can see I´m aplying the class col-md-6 in a weird way…

what happened, somebody edited my question?

Assuming you haven’t created a home or front-page template, then the loop will be in index template.

You can exclude a category from the loop with pre_get_posts (the Codex uses it as an example) and you don’t need one query for the first slide, and a second for the rest: just use a counter to change the markup.

Edit: Your question was edited to better display the code.

2 Likes

here is the right code for the slider:

<div class="row"> <div id="myCarousel" class="carousel slide" style="max-width: 1054px;">
 <div class="carousel-inner">
  <?php $count = 1; ?>
  <?php 
   $the_query = new WP_Query(array(
    'category_name' => 'Home Slider', 
    'posts_per_page' => 4 
    )); 
   while ( $the_query->have_posts() ) : 
   $the_query->the_post();
  ?><?php $class = ($count==1) ? 'active' : ''; ?>
   <div class="item <?php echo $class; ?>">
    <?php the_post_thumbnail('large');?>
    <div class="carousel-caption">
     <h4><?php the_title();?></h4>
     <p><?php the_excerpt();?></p>
    </div>
   </div><!-- item active -->
  <?php $count++; ?><?php 
   endwhile;  
   wp_reset_postdata();
  ?>
  
 </div><!-- carousel-inner -->
 <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
 <a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div><!-- #myCarousel -->  </div>

and it works, much more beautiful!

but I added this to custom.php wich seems to make no effect:

<?php
/**
 * Custom functions
 */
function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', 'Home Slider' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' ); ?>

You can’t use the category slug, you’ll need to use the negative ID.

$cat = '-' . get_cat_id('Home Slider');  
$query->set('cat', $cat);
1 Like

yessss! Thankyou hero

the custom function, in case someone wants to use it:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $cat = '-' . get_cat_id('categoryofyourchoice');  
$query->set('cat', $cat);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );