WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Loops (5 posts)

  1. Ryan Fitzer
    Member
    Posted 7 years ago #

    I'm trying to have two Loops on my index.php page with each one only displaying one category. Can anyone give me an idea where I'm going wrong. Here's the code:

    <?php query_posts('cat=1&showposts=2'); ?>

    <?php if (have_posts()) : ?>

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

    <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    <?php query_posts('cat=2&showposts=2'); ?>

    <?php if (have_posts()) : ?>

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

    <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    <?php else : ?>

    <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>

    <?php endif; ?>

    This gives me a parse error "unexpected $". I've read about multiple loops in the codex and on the forums but I'm still not getting it. Any help or advise would be appreciated. Thanks.

  2. Ryan Fitzer
    Member
    Posted 7 years ago #

    It figures. Right after I posted this I found my answer. Here is where I got it:

    http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html

    And here is how the loop is formatted:

    // Show only posts in category 1 "current"
    <?php query_posts('cat=1&showposts=2'); ?>

    <?php if (have_posts()) : ?>

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

    <div class="current" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    // Show only posts in category 2 "news"
    <? $my_query = new WP_Query('cat2&showposts=2'); ?>

    <? while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="news" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <? endwhile; ?>

    <?php else : ?>

    <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>

    <?php endif; ?>

    This is altered to only show the post title and post content. You'll have to put all the post meta back in if you want it to be back to the default.

  3. Ryan Fitzer
    Member
    Posted 7 years ago #

    Sorry. Scratch that last block of code. I forgot to put "php" in a few places. Here's the correct version:

    <?php query_posts('cat=1&showposts=2'); ?>

    <?php if (have_posts()) : ?>

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

    <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    </div>

    <div class="news">

    <h2>News and Events</h2>

    <?php $my_query = new WP_Query('cat2&showposts=2'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    <?php else : ?>

    <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>

    <?php endif; ?>

  4. greatbig47
    Member
    Posted 7 years ago #

    Where would you stick in the name of the category, if you wanted it to be dynamic? I.E...(In your case) If News and Events was a category, but it didn't have any posts (so it wouldn't appear)?

  5. Ryan Fitzer
    Member
    Posted 7 years ago #

    Not sure how that would work but I think I read something on it. I'll look around and post back when I get it.

Topic Closed

This topic has been closed to new replies.

About this Topic