Support » Fixing WordPress » Problem with loop in single.php

  • Resolved alvarols


    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>
                        <?php } ?>
                      <?php endwhile; ?>
                      <?php endif; ?>


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


    Forum Moderator

    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?

    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 ) ); ?>

    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.