WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loop inside loop: having an issue (3 posts)

  1. SpankMarvin
    Member
    Posted 4 years ago #

    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:

    get_header(); 
    
    jds_list_book_subpages($post->ID);
    $cat_list = get_post_meta($post->ID, "Include Category Listing (Enter slug or Cat ID)", $single = true);
    if(is_numeric($cat_list)){
    				$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>
    
    			</div>
    			<?php endwhile; // End of new loop
    
    			wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    		</div>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    </div>
    <?php get_footer(); ?>

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

  2. SpankMarvin
    Member
    Posted 4 years ago #

    P.S. I see there's an HTML entity typo in there, should be ” but that's not affecting the issue!

  3. SpankMarvin
    Member
    Posted 4 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.