Title: Problem with loop in single.php
Last modified: August 20, 2016

---

# Problem with loop in single.php

 *  Resolved [alvarols](https://wordpress.org/support/users/alvarols/)
 * (@alvarols)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/problem-with-loop-in-singlephp/)
 * I’m making a loop (using _multiple loops_) in order to show the related post 
   from the main article category. The problem is that the loop prints the last 
   posts from the month when I specified a category ID.
 * **Note:** I obtain the category ID before and it’s contained on the $categoria
   variable. If i “echo” the $categoria inside the loop, it shows me the correct
   ID, but i don’t know why is not working.
 * The same code works well on index.php but not here:
 * `<?php $events_query = new WP_Query(“‘cat=”.$categoria.”&posts_per_page=3′”);?
   >
                      <?php $postnum = 0;?>                   <?php if(have_posts()):?
   ><?php while($events_query->have_posts()) : $postnum = $postnum + 1; $events_query-
   >the_post(); ?>                     <?php if ($postnum > 1 ) { ?>            
   <div class=”cuadrorelated”>                             <div class=”imagenrelated”
   >                                 <?php if(has_post_thumbnail()) { ?>        
   <?php the_post_thumbnail(‘posthumb’); ?>                                 <?php}
   else {?>                                     <img src=”<?php bloginfo(‘template_directory’);?
   >/images/hemeroteca.jpg” />                                 <?php } ?>       
   <h1 class=”negrocentro”>“><?php the_title(); ?></h1>                         
   </div>                         </div>                     <?php } ?>         
   <?php endwhile; ?>                   <?php endif; ?>
 * Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/problem-with-loop-in-singlephp/#post-3326852)
 * try to remove the single quotes from the query parameters:
 *     ```
       <?php $events_query = new WP_Query("cat=".$categoria."&posts_per_page=3"); ?>
       ```
   
 * if this does not work, continue with the troubleshooting questions:
 * can you paste the **full **code of single.php into a pastebin and post the link
   to it here?
    [http://codex.wordpress.org/Forum_Welcome#Posting_Code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
 * does the code work if you replace `$categoria` with the ID of an existing category?
 * what happens if you replace the query line with:
 *     ```
       <?php $events_query = new WP_Query( array( 'category__in' => array($categoria), 'posts_per_page' => 3 ) ); ?>
       ```
   
 * [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [alvarols](https://wordpress.org/support/users/alvarols/)
 * (@alvarols)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/problem-with-loop-in-singlephp/#post-3326860)
 * Thank you, it worked fine. There were the quotes.
 * In this case i could not replace $categoria with an ID because that ID were generated
   automatically.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem with loop in single.php’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [id](https://wordpress.org/support/topic-tag/id/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [single](https://wordpress.org/support/topic-tag/single/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [alvarols](https://wordpress.org/support/users/alvarols/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/problem-with-loop-in-singlephp/#post-3326860)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
