• Hi,

    I’ve tried to implement multiple loops on my index.php template, by using the following code:

    <?php get_header(); ?>

    <div id="content" class="narrowcolumn">

    <h2>Recent News</h2>
    <br />

    <?php query_posts('category_name=News&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <i>Your display code goes here</i>
    <?php endwhile; ?>

    <h2>Recent Reviews</h2>
    <br />

    <?php query_posts('category_name=Reviews&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <i>Your display code goes here</i>
    <?php endwhile; ?>

    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    However, when I view the page, the category names I have used seem to fool the sidebar into thinking its on an archive page for the second category (Reviews). If I remove the second loop, it fools the sidebar into thinking its on an archive page for the first category (News).

    I don’t understand what I’ve done wrong here as I’ve followed the information found on https://infovore.org/archives/2005/04/12/multiple-loops/ to the letter. What am I missing out?

    Also, could someone tell me what it means by ‘display code’ in that example of multiple loops? No matter what I add gives me parse errors… apologies but I’ve been learning PHP and WP stuff for only a few days and I’ve a lot to catch up on.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think the “display code” means the actual tags you want to show up, such as:

    <div class="post">
    <?php the_date('','<h2>','</h2>'); ?>
    <h3 class="storytitle" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="storycontent">
    <?php the_content(); ?>
    </div>
    <?php the_meta(); ?>

    <div class="meta"> or so <?php the_author() ?> said @ <?php the_time() ?> </div>
    <div class="meta"><?php _e("Filed under:"); ?> <?php the_category() ?></div>

    As it is, you’re not telling it what to display.

    Try resetting the query to the default after using your custom queries. It should be documented in the Codex.

    Thread Starter mookboy

    (@mookboy)

    *slaps forehead*

    Sorry…. right in that case I NOW have working content (Hurrah!), however I’m still stuck with a wonky sidebar. Ive tried adding the rewind code <?php rewind_posts(); ?> from the codex (is this what you meant skeltoac?) but it has no effect (or I’m putting it in the wrong place?).

    Maybe I need to give you more info (sorry). When it renders the sidebar on the index.php, it shows up with the archive, shortened sidebar, and has “You are currently browsing the archives for the Reviews category” below the search field. I’m baffled.

    Mookboy, that’s not it. What I meant was this: by calling query_posts(), you have changed the data cached by the $wp_query object; you must set $wp_query back to its original state as directed by the URL requested. I remember reading a way around this, where you make a copy of $wp_query before calling query_posts(). This is NOT EXACT:
    global $wp_query;
    $wp_query_backup=$wp_query; // make a backup copy
    query_posts([whatever]); // this alters $wp_query
    [whatever] // do your thing
    $wp_query=$wp_query_backup; // restore from the backup
    unset($wp_query_backup); // optional

    You might benefit from studying the WP_Query class in classes.php.

    Thread Starter mookboy

    (@mookboy)

    Woah, that’s well beyond my limited understanding I’m afraid. Would I be right in thinking I enclose the code I’m using:

    <?php query_posts('category_name=News&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php c2c_get_recent_posts(15, "<small>%post_date%:</small> %post_URL%", "2"); ?>
    <?php endwhile; ?>

    With a more exact version of the code you just suggested?

    I don’t suppose you know where I can find exact code to do this? My ability with php and wp is meagre to say the least…

    Read about the Loop:
    https://codex.www.ads-software.com/The_Loop
    see the examples for multiple loops

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple Loop Weirdness’ is closed to new replies.