Title: Confused with The Loop
Last modified: August 21, 2016

---

# Confused with The Loop

 *  Resolved [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/)
 * Hi there guys, quick question, I have a category.php page. How it should work
   its pretty easy, when you click a category you’ll land on this page with all 
   the posts associated to that category, however, there’s something wrong because
   it is printing all the posts no matter what category you are clicking on, this
   is part of my code, or at least, the code relevant to this question:
 * First I’m printing the Category name – This is actually working –
 * `<div class="breadcrumbs-categories"><h1>NOTICIAS DE <?php echo single_cat_title();?
   > EN ESPAÑOL</h1></div>`
 * The The Loop – Which is not working –
 *     ```
       <?php
   
       global $post;
   
       $args = array( 'numberposts' => 3 );
   
       $myposts = get_posts( $args );
   
       foreach( $myposts as $post ) :	setup_postdata($post); ?>
   
       <!-- START CONTENTS 1 -->
   
       <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
       <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
   
       <!-- Start Contents -->
   
       <div class="maincontents-categories">
   
       <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
   
       <p> </p>
   
       <a href="<?php the_permalink(); ?>" class="readmore">Leer más...</a>
   
       </div>
   
       <?php endforeach; ?>
       ```
   
 * What Am I missing?? When I call a category it prints the category name but prints
   the first 4 posts no matter the category, any idea?
 * Thanks!
 * Arturo

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/confused-with-the-loop/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/confused-with-the-loop/page/2/?output_format=md)

 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939504)
 * Just noticed that I pasted the wrong code, so here I go again, same problem and
   this time this is the right code I’m using in my category page:
 *     ```
       <div id="content-categories">
   
       <div class="breadcrumbs-categories"><h1>NOTICIAS DE <?php printf( __( '%s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?> EN ESPAÑOL</h1></div>
   
       <?php query_posts('showposts=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>
   
       <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
   
       <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
   
       <div class="maincontents-categories">
   
       <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
   
       <p>&nbsp;</p>
   
       <a href="<?php the_permalink(); ?>" class="readmore">Leer m&aacute;s...</a>
   
       </div>
   
       <?php endwhile;?>
   
       <?php else : ?>
   
       <h1>Nothing Found</h1>
   
       <?php endif; wp_reset_query(); ?>
       ```
   
 * Thanks in advance!
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939508)
 * Might be a little overkill but try this and let me know if it works:
 *     ```
       <?php $my_query = new WP_Query('showposts=3');
         while ($my_query->have_posts()) : $my_query->the_post();
         $do_not_duplicate[] = $post->ID ?>
       ```
   
 * Almost forgot, end it with this:
 *     ```
       <?php endwhile; ?>
       ```
   
 * instead of what you currently have.
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939510)
 * Hey doc4, nope, it shows the same thing.
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939511)
 * Try this just to see if it separates the posts:
 * `<?php if (have_posts()) : while (have_posts()) : the_post(); ?>`
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939512)
 * Well not sure because it throws me an error on line 64, I have this on that line:
 * <?php get_footer(); ?>
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939514)
 * Actually, having the following it separates the posts, but, now I have 10 posts
   instead the 3 I need in that section:
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   
       <!-- START CONTENTS 1 -->
   
       <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
   
       <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
   
       <!-- Start Contents -->
   
       <div class="maincontents-categories">
   
       <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
   
       <p>&nbsp;</p>
   
       <a href="<?php the_permalink(); ?>" class="readmore">Leer m&aacute;s...</a>
   
       </div>
   
       <?php endwhile;?>
   
       <?php else : ?>
   
       <h1>Nothing Found</h1>
   
       <?php endif; wp_reset_query(); ?>
       ```
   
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939515)
 * Okay, save your code in a text editor and replace it with this just to see if
   it’s working. I’m trying to get rid of anything not related and break this down
   to the basics. Leave the header and footer code of course but if you have another
   loop remove it for now.
 *     ```
       <div id="content-categories">
   
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <div class="maincontents-categories">
   
       <p><?php the_title(); ?></p>
   
       </div>
   
       <?php endwhile; endif; ?>
       ```
   
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939516)
 * Yup, that one works but works like in my past reply, just throws me all 10 posts,
   breaking my head here and don’t understand what might be wrong.
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939517)
 * Okay, hang on a second
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939518)
 * You probably have the max posts set to ten under settings which is why you’re
   seeing ten total.
 * Anyway try this:
 *     ```
       <div id="content-categories">
   
       <?php query_posts('showposts=3'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <div class="maincontents-categories">
   
       <p><?php the_title(); ?></p>
   
       </div>
   
       <?php endwhile; endif; ?>
       ```
   
 * Yes, I realize this is basically what you had before.
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939519)
 * Yup I know, the thing is, in a few pages I’ll need more than 10, in others just
   3 or 5 of them, anyway, tried that one and no, it just output 3 posts but it 
   doesn’t separate them, always the same 3 posts, the weird thing is this, it prints
   the right category name, for that I’m using:
 * <?php printf( __( ‘%s’ ), ‘<span>’ . single_cat_title( ”, false ) . ‘</span>’);?
   >
 * But that’s it, gee!, where’s the vodka when needed?
 *  [doc4](https://wordpress.org/support/users/doc4/)
 * (@doc4)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939522)
 * Wait a minute, I just realized to make this work we probably need to tell the
   page what categories we are specifically wanting to see. So you would normally
   split this page into several IF/THEN statements. IF category 1 show posts from
   category 1. Then another loop for another category.
 * Try this, hopefully my code is somewhat sound. Replace the two names of the categories
   with the categories you’re wanting to display.
 *     ```
       <?php
       if (in_category('1')) {
         $category_name = 'your_category_name';
       } else if (in_category('2')) {
         $category_name = 'your_second_category_name';
       }
       query_posts( "category_name=$category_name&posts_per_page=3"); ?>
       <?php while (have_posts()) : the_post(); ?>
   
       <div class="maincontents-categories">
   
       <p><?php the_title(); ?></p>
   
       </div>
   
       <?php endwhile; endif; ?>
       ```
   
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939523)
 * It is throwing me an error on line 40 which is:
 * <?php endwhile; endif; ?>
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939536)
 * Try it by removing the query from the category.php file and query with this in
   your theme’s functions.php:
 *     ```
       function my_post_queries( $query ) {
         // not an admin page and is the main query
         if (!is_admin() && $query->is_main_query()){
   
           if(is_category()){
             $query->set('posts_per_page', 3);
           }
   
         }
       }
       add_action( 'pre_get_posts', 'my_post_queries' );
       ```
   
 * [http://www.billerickson.net/customize-the-wordpress-query/](http://www.billerickson.net/customize-the-wordpress-query/)
   
   [http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/](http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/)
   [http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts](http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts)
 *  Thread Starter [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * (@arturocivit)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/#post-3939587)
 * Hi keesiemeijer, good day!
 * That did the trick, and seems like it works nicely, however, in that document,
   where the 3 posts are appearing there’s a sidebar with a different design elements
   where it needs to have 8 posts of the same category, calling the posts there 
   just output 3 because of the function, what should I do, create a new function
   with a different name with more posts to be displayed? Thanks so much for the
   help!

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/confused-with-the-loop/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/confused-with-the-loop/page/2/?output_format=md)

The topic ‘Confused with The Loop’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 29 replies
 * 3 participants
 * Last reply from: [arturocivit](https://wordpress.org/support/users/arturocivit/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/confused-with-the-loop/page/2/#post-3939621)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
