SparrwHawk
SparrwHawk

Reputation: 14153

WordPress Multiple Loops

I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...

<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

Upvotes: 0

Views: 2080

Answers (6)

laraborg
laraborg

Reputation: 1

There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page

https://codex.wordpress.org/Function_Reference/rewind_posts

Upvotes: 0

SparrwHawk
SparrwHawk

Reputation: 14153

I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)

<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>

<?php wp_reset_postdata(); // reset the query ?>

<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>

Upvotes: 0

Luca Reghellin
Luca Reghellin

Reputation: 8101

I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?

This will help me aswering more correctly.

Upvotes: 0

Mark Nguyen
Mark Nguyen

Reputation: 7448

This will work:

<?php
    global $query_string;        //make sure these are in the correct format for post queries
    global $query_string1;

    // First loop
    $first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');

    while ( $first_loop->have_posts() ) : $first_loop->the_post() :
        // do your thing
        // e.g: $first_loop->the_content();
    endwhile;


    // second loop
    $second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');

    while ( $second_loop->have_posts() ) : $second_loop->the_post() :
        //do your thing
    endwhile;

    // Reset Post Data 
    wp_reset_postdata();

?>

Upvotes: 0

campino2k
campino2k

Reputation: 1661

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

  // your output html   

<?php endwhile; ?>

You have nested the loops (missing endwhile).

Upvotes: 1

vzwick
vzwick

Reputation: 11044

You cannot just invent a variable "query_string2" and expect it to work ;)

Try this:

<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:

<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

Upvotes: 0

Related Questions