Multiple loop issue
-
Hi. I’m having an issue using multiple loops in an archive.php template.
I’m using a custom loop combined with the default loop of wordpress.
In the fist part, I want to call posts that are in both categories “features” (cat id=4) and “surf” (cat id=5). So, I use a custom loop that makes this to work.
NOTE: I also use the “features” category in a custom loop at home page. So, in home page I call ALL the posts in features category, don’t care if is in surf, skate or snow categories.In the second part I call (using default loop function) all the post in current category, excluding posts in features category for current category (because I call it in first loop, of course). But…I’m having an issue with post_per_page query string function. I want to display 4 post per page under features posts. But its have a conflict with displaying post because of post in features category. I only get 4 minus X features posts. So…if I have 2 features posts, I get only 2 posts displayed.
I know that if I set showposts=1 for features post and put post_per_page = 5…I will get 4 posts under features posts.
But the trouble is that I want to post X number of features posts because I want to make a features posts slideshow via javascript. So I need to display always 4 posts under features posts, don’t matter how many features post I have. I was looking for a solution at wordpress forums and other blogs, but I can’t find a solution (maybe I’m doing something wrong).Basically, I need to exclude features post from default loop for archive.php template and always show 4 “normal” posts.
Here is my code:
<-- custom loop to display features surf news. Always on top in all category pages --> <?php if (is_category('surf')) { ?> <?php query_posts(array('category__and' => array(4,5))); ?> <?php if (have_posts()) : ?> <div id="features-surf"> <div class="features-container" > <?php while (have_posts()) : the_post(); ?> <--- my stuff ---> <?php endwhile; ?> </div> </div> <?php endif; ?> <?php } ?> <--- default loop to display remaining posts in surf category. 4 posts per page --> <?php query_posts($query_string.'&posts_per_page=4'); ?> <?php if (have_posts()) : ?> <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?> <?php while (have_posts()) : the_post(); ?> <?php if (in_category('features')) continue; ?> <--- my other stuff ---> <?php endwhile; endif?>
Sorry for to much long post and my bad english.
Does anyone can help me? Thanks in advance
- The topic ‘Multiple loop issue’ is closed to new replies.