• Hi all,

    I’m trying to implement a second loop in one of my single.php pages (for a set category, in this case ‘images’, id 9). Nothing is being returned, though when I fiddle with the second query to change the parameters, I get strange results

    Here’s my first loop:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <p>   <?php the_content(); ?></p>
    
    <?php endwhile; endif; ?>

    and the second:

    <?php while(have_posts()) : the_post(); ?>
    
    		<?php foreach((get_the_category()) as $category)
                              { $my_query = new WP_Query('category_name=images' . $category->category_nicename . '&orderby=title&order=desc&showposts=5');} ?> 
    
                           <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
             <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    			<?php the_excerpt(); ?>
    		</a>
    
                          <?php endwhile; ?>
     		<?php break; endwhile; ?>

    If I leave category_name blank, I get all posts from all categories – this makes me think I must be close (after HOURS!) so any help greatly appreciated!

    Thanks

Viewing 1 replies (of 1 total)
  • why is your second loop wrapped into <?php while(have_posts()) : the_post(); ?> ?

    are you trying to do the second loop for each post category?

    this won’t work, as this is kind of running for its own purpose, and probably only returns the category object for the last post category:

    <?php foreach((get_the_category()) as $category)
                              { $my_query = new WP_Query('category_name=images' . $category->category_nicename . '&orderby=title&order=desc&showposts=5');} ?>

    do you have post categories with the name ‘imagesCategoryNiceName’?
    as this construct tries to do:
    'category_name=images' . $category->category_nicename . '&....

    let’s hope i understand this right, and assume you only have one category with the post (then the foreach does ok) and you want to show 5 posts of the category ‘images’ then you could try (this would still leave the category_nicename from ‘get_the_caegory()’ part unused):

    <?php
    foreach((get_the_category()) as $category)
                              { $my_query = new WP_Query('category_name=images&orderby=title&order=desc&showposts=5');} ?> 
    
                           <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
             <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    			<?php the_excerpt(); ?>
    		</a>
    
                          <?php endwhile; wp_reset_query(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Multiple Loops on single.php – no posts returned’ is closed to new replies.