• Resolved alvarols

    (@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>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
    ? ? ? ? ? ? ? ? ? ? ? ? </div>
    ? ? ? ? ? ? ? ? ? ? <?php } ?>
    ? ? ? ? ? ? ? ? ? <?php endwhile; ?>
    ? ? ? ? ? ? ? ? ? <?php endif; ?>

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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?
    https://codex.www.ads-software.com/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 ) ); ?>

    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter alvarols

    (@alvarols)

    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.