WordPress.org

Forums

Multiple loop problem in single.php (6 posts)

  1. fredhead
    Member
    Posted 6 years ago #

    Hi,

    I've read the multiple loop info here:

    http://codex.wordpress.org/The_Loop#Multiple_Loops

    I've got one loop inside the larger loop in single.php. The internal loop simply outputs a linked TOC to other articles tagged with the same category. Per the documentation, for the TOC loop, I've set up a custom query object. The TOC is in a DIV right after the page title is output.

    However, when the page is run the single page title is correct, the TOC loop output is correct, but the larger container loop then outputs content and related data for the earliest post tagged with the same category as the title. So the title changes properly as I click through the TOC links but the post content remains the same (the earliest post tagged with the category). The loop is not reverting back to the larger container loop query.

    Here's the code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?			$category = get_the_category();
    			$storycatid = $category[0]->cat_ID;
    ?>
    
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<h2><?php the_title(); ?></h2>
    
    			<div class="entry">
    
    <?php
    		if ($parentcatid == '32') {
    			$rdtoc_query = new WP_Query('showposts=15&cat='.$storycatid);
    			if ($rdtoc_query->have_posts()) : ?>
    			<div>
    				<ul>
    <?				while ($rdtoc_query->have_posts()) : $rdtoc_query->the_post(); ?>
    			<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php			endwhile; ?>
    				</ul>
    			</div>
    <?			endif;
    		}
    ?>
    
    				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
    
    ...rest of single.php code is untouched...

    Any ideas how to reset the_content to display the proper data would be appreciated, thanks!

  2. iridiax
    Member
    Posted 6 years ago #

    Have you tried it as non-nested loops?

  3. fredhead
    Member
    Posted 6 years ago #

    Well, that's what I would like. The problem is that I want/need the TOC DIV to be between the title and the content (because the title is in an H2 and I can't get the H2 to float to the right of the TOC DIV along with the content (which floats fine). The H2 insists on breaking to a new line or it sits under the TOC DIV and refuses to move.

    In a perfect world, the TOC DIV would output then the post would output. I can't get that to work so I'm trying to do what I thought was a fairly simple multiple loop. I just need to figure out how to reset the query data after the TOC loop outputs, to return to the original wp query.

    Thanks for any ideas...

  4. iridiax
    Member
    Posted 6 years ago #

    To avoid nesting, could you use a variable to store the query results?

    http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2

  5. fredhead
    Member
    Posted 6 years ago #

    I tried that before posting here and it failed, too. I got the same effect: running the customized TOC query somehow knocked out the larger query. That's why I'm stumped. It seems so obvious, and straightforward. But it's not working...

  6. fredhead
    Member
    Posted 6 years ago #

    Hi,

    I'm trying a hack where I split these multiple loops into 2 loops where the TOC loop generates a variable used in the main (now single) loop.

    However, when I assign the template tags to a PHP varialble, for example, the_title(), in my PHP it outputs the template tag rather than load the variable. Can someone help me tweak my PHP to make it work?

    Here's the code:

    <?php
    		if ($parentcatid == '32') :
    			$rdtoc_query = new WP_Query('showposts=15&cat='.$storycatid);
    			if ($rdtoc_query->have_posts()) :
    				$toc = "<div style=\"background: #eee; width: 270px; float: left; margin: 0 10px; padding: 10px; position: relative;\">\n";
    				$toc .= "<h3>".$parentcat."</h3>\n";
    				$toc .= "<p><em>".$storycatname."</em></p>\n";
    				$toc .= "<ul>\n";
    				while ($rdtoc_query->have_posts()) : $rdtoc_query->the_post();
    					$permalink = the_permalink();
    					$linkattribute = the_title_attribute();
    					$linktitle = "Permanent Link to ".$linkattribute;
    					$title = the_title();
    					$toc .= "<li><a href=\"".$permalink." rel=\"bookmark\" title=\"".$linktitle."\">".$title."</a></li>\n";
    				endwhile;
    				$toc .= "</ul>\n";
    				$toc .= "</div>\n";
    			endif;
    		endif;
    		echo $toc;
    ?>

    Again, this outputs the right code I want, just not the WordPress values.

    Thanks for any help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.