WordPress.org

Ready to get started?Download WordPress

Forums

multiple loops with if statement syntax question (6 posts)

  1. rstarr
    Member
    Posted 7 years ago #

    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:

    <?php
    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?

    Thanks!!

  2. moshu
    Member
    Posted 7 years ago #

    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 :)

  3. rstarr
    Member
    Posted 7 years ago #

    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?

  4. moshu
    Member
    Posted 7 years ago #

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

  5. rstarr
    Member
    Posted 7 years ago #

    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; ?>
    </div>

  6. ColourDreamer
    Member
    Posted 6 years ago #

    No answer??

Topic Closed

This topic has been closed to new replies.

About this Topic