Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multiple content loops on same page WordPress

  • SOLVED

Hi Experts,

I am trying to use multiple content loops within the same page template. Essentially what I have is a rotating banner area on the homepage. This area will display a set number of posts within a certain category. It also has 2 layout variations based on whether a post-thumbnail is present or not. This is working fine, but down below this banner area, I also have a content area that uses another standard Loop that just calls in the content. The problem I'm running into is that the 2nd loop is not functioning properly. The 2nd loop is pulling in posts from the category specified by the 1st loop. I just want it to pull in the regular page content.

Sorry if this is confusing. Please let me know if you have any questions.

Thanks a lot for your help.


<strong>Here's the code: </strong>

<!-- LOOP 1 -->

<div id="home_slider">
<?php
$feathome = // get_option('ss_feathome_cat');
$item_count_home = 4; // get_option('ss_homeitems');
$home_category_id = 32; // get_cat_id($feathome);
$query_string ="posts_per_page=$item_count_home&cat=$home_category_id";

query_posts($query_string);

if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php if ( has_post_thumbnail() ) : ?>
<div class="slider-container">
<div class="home_slider_thumbnail_content">
<?php the_content(); ?>
</div><!-- end home_slider_thumbnail_content -->

<div class="home_slider_thumbnail"><?php the_post_thumbnail('Homefeatimg'); ?>
<div class="home_slider_bottom">&nbsp;</div><!-- end bottom -->
</div><!-- end home_slider_thumbnail -->
</div><!-- end slider-container -->


<?php else : ?>


<div class="slider-container">
<div class="home_slider_thumbnail_content_wide">
<?php the_content(); ?>
</div><!-- end home_slider_thumbnail_content_wide -->
</div><!-- end slider-container -->

<?php endif; ?>
<?php endwhile; endif; ?>

</div><!-- end home_slider -->





<!-- LOOP 2 -->


<div id="content_wrap">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php the_content(); ?>

Answers (2)

2010-04-09

Mathieu Hallé answers:

to create mulitple loops on 1 page you have to:

1- try to never place a loop within a loop.
2- always use wp_reset_query(); after a loop.




<?php
// -------------------------- Loop 1--------------------------
$args = array(
'posts_per_page' => 1,
'post_parent' => 2,
'post_type' => 'page',
'orderby' => 'menu_order'

);

query_posts($args);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<?php the_title(); ?>">
<?php the_post_thumbnail( 'home_primeur' ); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); //-------------------------- IMPORTANT ?>




// -------------------------- Loop 2--------------------------
$args = array(
'posts_per_page' => 5,
'post_parent' => 3,
'post_type' => 'page',
'orderby' => 'menu_order'

);

query_posts($args);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<?php the_title(); ?>">
<?php the_post_thumbnail( 'home_primeur' ); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); //-------------------------- IMPORTANT ?>






// -------------------------- Regular page loop --------------------------
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<?php the_title(); ?>">
<?php the_post_thumbnail( 'home_primeur' ); ?>

<?php endwhile; ?>
<?php endif; ?>








you can check the codex for a list of all the arguments you can pass to query_posts() to get only the post/page you want

http://codex.wordpress.org/Function_Reference/query_posts


hope it help





2010-04-09

Buzu B answers:

If you only need to get the content of one single post, you can use the global $wpdb to pull the post directly from the data base.

Tell me how you decide how to show the post in the second loop lo help you a bit more...


WP Answers comments:

Well, the second loop isn't actually pulling in a post. Its just pulling in the pages content. So for instance if the user creates a new page, and chooses the 'homepage' template, the second loop should pull in whatever content they put in the content area of the WYSIWYG area for that page.

Does that help?


Buzu B comments:

Ok, i think I understand what you mean.

try adding this:

rewind_posts();

just before the:

if(have_posts())

of the second loop


WP Answers comments:

That didn't do it. Is this the correct way to do it? I tried this way and also tried putting it right before the if statement bu neither worked.


<?php rewind_posts(); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>



As per the logic of the content, just pretend the 1st loop doesn't exist, and there's only one content area on the page, and the 2nd loop would be pulling in that content.


Buzu B comments:

Ok it seems like you will have to create a new query object. That will keep the original query alive.

Use this:

$new_query = new WP_Query($query_string);

instead of

query_posts($query_string);

And then,

use this loop instead of the one you have:

while ($new_query->have_posts()) : $new_query->the_post();

Something like this:

query_posts($query_string);



if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post(); ?>
<?php if ($new_query->has_post_thumbnail() ) : ?>
<div class="slider-container">
<div class="home_slider_thumbnail_content">
<?php $new_query->the_content(); ?>
</div><!-- end home_slider_thumbnail_content -->
<div class="home_slider_thumbnail"><?php $new_query->the_post_thumbnail('Homefeatimg'); ?>
<div class="home_slider_bottom">&nbsp;</div><!-- end bottom -->
</div><!-- end home_slider_thumbnail -->
</div><!-- end slider-container -->
<?php else : ?>
<div class="slider-container">
<div class="home_slider_thumbnail_content_wide">
<?php $new_query->the_content(); ?>
</div><!-- end home_slider_thumbnail_content_wide -->
</div><!-- end slider-container -->
<?php endif; ?>
<?php endwhile; endif; ?>


And leave the first loop as it was at the beginning.