• Hi

    I need 3 separate loops on my category pages – one that shows the latest 5 posts from the category, another that shows the 5 before that, and then another that shows the 12 before that. I have 42 categories.

    Currently, for each section, I am running through all 42 categories in one huge if statement. That means 3 huge if statements for each category page. I find it hard to believe that I am doing it in the most efficient way! Any help would be greatly appreciated 🙂

    Example of the current code:

    <?php if (is_category('1')) : ?>
    
    <?php $recent = new WP_Query("cat=1&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    "><?php the_title(); ?>
    <?php endwhile; ?>
    
    <?php elseif (is_category('2')) : ?>
    
    <?php $recent = new WP_Query("cat=2&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    "><?php the_title(); ?>
    <?php endwhile; ?>
    
    ...
    
    <?php elseif (is_category('42')) : ?>
    
    <?php $recent = new WP_Query("cat=42&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    "><?php the_title(); ?>
    <?php endwhile; ?>
    
    <?php endif; ?>
Viewing 1 replies (of 1 total)
  • Thread Starter goatgash

    (@goatgash)

    Mangle the code above – should read:

    <?php if (is_category('1')) : ?>
    
    <?php $recent = new WP_Query("cat=1&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    
    <?php elseif (is_category('2')) : ?>
    
    <?php $recent = new WP_Query("cat=2&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    
    ...
    
    <?php elseif (is_category('42')) : ?>
    
    <?php $recent = new WP_Query("cat=42&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    
    <?php endif; ?>
Viewing 1 replies (of 1 total)

The topic ‘multiple loops > php help’ is closed to new replies.