• Resolved Archie Makuwa

    (@archie22is)


    Hey guys,

    I have a two custom post types:
    – Products
    – and Symptoms

    The products are displayed in a excerpt format on the products page and grouped according the products category (custom taxonomy). The code is pasted on Pastebin on the following url: https://pastebin.com/K1TvtgBA

    The product single page is as follows: https://pastebin.com/ZJeRauJT

    The Question:

    I want to run a custom query (I have tried: WP_Query & query() ) to return the custom posts within the associated symptoms div on the single page. It works, but breaks my single page loop. The moment I run the loop, I am unable to display:
    – Indications
    – and Contra-Indications

    What is the best method to run the loop within the already existing loop. What the symptoms actually do is retrieve a bunch of related symptoms (related to product based on custom taxonomy).

    Someone said that I should not use (the_post) as that is the default WordPress post – tried that and I obtained the same results.

    Any additional suggestions?

Viewing 1 replies (of 1 total)
  • Thread Starter Archie Makuwa

    (@archie22is)

    There was no need to run a loop within a loop on the single page. I used the formula below:

    <?php
    	$terms = get_the_terms( $post->ID, 'symptoms_cat' );
    	foreach ( $terms as $term ) {
    		$x = $term->name;
    	}
    ?>
    
    	<!-- Execute loop -->
    	<?php
    		query_posts(
    			array(
    				'post_type' => 'symptom',
    				'category_name' => $x,
    				//'category_name' => '',
    				'posts_per_page' => 99
    			)
    		);
    	?>
    
    	<ul class="symptoms-list">
    		<?php while (have_posts()) : the_post(); ?>
    			<?php
    	        	$symptom_img = get_post_meta($post->ID, 'symptom_image', true);
    	          	$my_images = $symptom_img['guid'];
    	    	?>
    			<li>
    				<a href="<?php the_permalink(); ?>">
    	           		<img src="<?php echo $my_images; ?>" /> <br />
    					<?php the_title(); ?>
    				</a>
    			</li>
    		<?php endwhile; ?>
    	</ul>

    Any questions or queries, holla back!!

Viewing 1 replies (of 1 total)
  • The topic ‘Run WordPress loop within a loop’ is closed to new replies.