WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with loop in single.php (3 posts)

  1. alvarols
    Member
    Posted 1 year ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

    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

  3. alvarols
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic