Support » Fixing WordPress » Loop inside loop: having an issue

  • Resolved SpankMarvin


    Hi all

    I’m trying to place a loop within a page’s loop. It basically picks up from a custom field, and generates an additional list accordingly. Problem is, Where my new loop starts, no more code is ouputted in the php (so I get the opening of the original loop and that’s where the page terminates). I’m not sure what I’m doing wrong, but here is my page template code:

    $cat_list = get_post_meta($post->ID, "Include Category Listing (Enter slug or Cat ID)", $single = true);
    				$cat_query = 'cat='.$cat_list;
    			} else {
    				$cat_query = 'category_name='.$cat_list;
    			$cat_listing = new WP_Query($cat_query.'&orderby=ID&order=asc');
    <div id="content">
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<h2><?php the_title(); ?></h2>
    			<?php the_content('<p>Read the rest of this page &raquo;</p>');
    			// New loop starts here
    			while ($cat_listing->have_posts()) : $cat_listing->the_post(); ?>
    			<div class="resource-list-item" id="resource-item-<?php $cat_listing->the_ID(); ?>">
    			<p>Full listing for resource &ldquo;<?php $cat_listing->the_title(); ?>&rquo; here...</p>
    			<?php endwhile; // End of new loop
    			wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    <?php get_footer(); ?>

    Any ideas? I’d appreciate any advice on this. Thought it would have been straightforward, but tired eyes, etc. etc.

Viewing 2 replies - 1 through 2 (of 2 total)
  • P.S. I see there’s an HTML entity typo in there, should be ” but that’s not affecting the issue!

    My bad, obviously. The issue was that I needed to drop the $cat_listing-> from the functions within the second loop. All running as expected now 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Loop inside loop: having an issue’ is closed to new replies.