Support » Everything else WordPress » multiple loops with if statement syntax question

  • I multiple loops running on one page. The loop I need help with is this:

    <?php $my_query = new WP_Query(‘category_name=client-list&showposts=1’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    It works fine. What i want to do, is say “if on specific page, then do a new query for a specific category name. So it’s basically amalgamating the above code with this code:

    if (is_home()) {
    do the above loop;

    And then to make it better, I want to have multiple if statements (for 5 different pages that will show a specific category loop). E.g if on “clients” page, make a new query for the category “people”, if on “home” page make a new query for “news” category. I’m using new query, because there is already a loop for the page content.

    Anyone good at syntax that can put this together?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Isn’t it simpler to use Category Templates? What you described in a complicated way… sounds like what the Cat Template does out of the box 🙂

    Yes, but i have pages with page content that is called from the main loop in the page (based on page.php). Depending on what page it is, i want a second loop in the page to call a specific category loop. So the secondary loop code in the page template would say, if this page is A, loop category X, if this page is B, loop category B. so all my code is in one page template.

    I’m not sure how category templates would work here because i’m not calling a category page, rather a page based on page.php

    does this make sense?

    No, it doesn’t. That’s why it doesn’t work: Pages are not meant to display multiple posts.

    Ok, how would i write the code just for this:
    If on page A, run this loop:

    <div id=”pix”>
    <?php $my_query = new WP_Query(‘category_name=pix-1-photos&showposts=1’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php echo $post->post_content; ?>
    <?php endwhile; ?>

    No answer??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘multiple loops with if statement syntax question’ is closed to new replies.