• I have a problem with multiple loops inside my custom template:

    <?php
    get_header();
    
    $my_query = new WP_Query('showposts='.$slide_count.'&cat=5');
    while ($my_query->have_posts()) : $my_query->the_post();
    	//do something
    endwhile;
    
    $category_content = get_post_meta($post->ID, 'category_content', true);
    $my_query = new WP_Query('orderby=ID&order=ASC&cat='.$category_content);
    while ($my_query->have_posts()) : $my_query->the_post();
    	?>
    	<h2><?php the_title() ?></h2>
    	<?php the_content() ?>
    	<?php
    endwhile;		
    
    get_footer(); ?>

    The above is a example of it. After the first while I try to access the property $post->ID in the get_post_meta. I would like the ID of the page I’m showing – but I get the last ID from the above loop. How can I prevent the loops from overwriting the loop that WordPress generates?

    Please tell me, if you do not understand my problem. I think is a bit complicated to explain ??
    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter DennisMadsen

    (@dennismadsen)

    I found a hack, but I don’t know if it’s a good solution:

    <?php get_header();
    echo "Post id: ".$post->ID."<br/><br/>";
    
    $category = get_the_category();
    $my_query = new WP_Query('cat='.$category[0]->cat_name);
    while ($my_query->have_posts()) : $my_query->the_post();
    	echo get_the_title()."<br>";
    endwhile;
    
    $post = $wp_query->post;
    
    echo "<br/>Post id: ".$post->ID;
    
    get_footer(); ?>

    Because of the $post = $wp_query->post;, I can go back to the original $post, after the multiple loops has ended. Are there other solutions? The post id printed in the top and bottom should be the same.

Viewing 1 replies (of 1 total)
  • The topic ‘Multiple loop: problem with $post’ is closed to new replies.