2 column multi loop
-
I’m trying to design a 2 column newspaper-style theme with a unique aspect: All even posts are displayed in the left column, while all odd posts are displayed in the right column.
What I’m having a hard time doing is the split. Getting a multi-loop split via categories is easy, but how do you identify even or odd posts and exclude either from a loop?
Thanks
-
I don’t think that’s a viable idea, unless you can define what is an even and what is an odd post.
Here is a dual-loop structure which works off the default posts query and should do what you’re asking:
<div class="odd-column"> <?php while(have_posts()) : ?> <?php $postcount++; if( ($postcount % 2) == 0 ) : // skip 'even' posts $wp_query->next_post(); else : ?> <?php the_post(); ?> ~ posts go here ~ <?php endif; ?> <?php endwhile; ?> </div> <?php $postcount = 0; rewind_posts(); ?> <div class="even-column"> <?php while(have_posts()) : ?> <?php $postcount++; if( ($postcount % 2) != 0 ) : // skip 'odd' posts $wp_query->next_post(); else : ?> <?php the_post(); ?> ~ posts go here ~ <?php endif; ?> <?php endwhile; ?> </div>I stand corrected š
For PHP gurus like Kaf… everything is possible!Well, not *everything*.
;)AWESOME! I’ll check it right now!
how do you think the comments work on this site or other sites, with the alternating colours š
if you can think of it, it can be done… somehow.
It’s working great.
To make things look as clean as possible, I’ve set the main loops to the_excerpt in index.php. Since pages default to index.php, is that a good idea? Or should I make a separate template for the main page, and code index.php normally?
I’ll have to wrap my head around how single.php and page.php, etc., will be formatted, but that’s just a matter of time now that I have this part down.
Thanks again!
“Or should I make a separate template for the main page, and code index.php normally?“
Yes. That is, I would move the dual-loop stuff over to a home.php, and allow index.php to act as a fall-back for any post query types that need it.
dear Kaf,
same as the above, can u guide me on how to make a 3 multicolumn loop? for example take a look at this site reformrevolution[dot]com. i’ve tried the method from perishablepress[dot]com but when i click the next page, it returns the same post. here’s the code :
// FIRST LOOP: display posts 1 thru 5
<?php query_posts(‘showposts=5’); ?>
<?php $posts = get_posts(‘numberposts=5&offset=0’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == “5”) { break; } else { ?><?php the_title(); ?>
<?php the_content(); ?><?php $count1++; } ?>
<?php endforeach; ?>// SECOND LOOP: display posts 6 thru 10
<?php query_posts(‘showposts=5’); ?>
<?php $posts = get_posts(‘numberposts=5&offset=5’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == “5”) { break; } else { ?><?php the_title(); ?>
<?php the_content(); ?><?php $count2++; } ?>
<?php endforeach; ?>// THIRD LOOP: display posts 11 thru 15
<?php query_posts(‘showposts=5’); ?>
<?php $posts = get_posts(‘numberposts=5&offset=10’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == “5”) { break; } else { ?><?php the_title(); ?>
<?php the_content(); ?><?php $count3++; } ?>
<?php endforeach; ?>thank you..
interested to see if you found an answer to this question. I’m having a similar problem.
I was wondering if there was a more elegant solution to this problem because one if/else should have been enough to get this done… I came across this solution:
“Replace have_posts with this in the loop:
<?php
$x=0;
if (have_posts()) : ?>Add a check and condition for odds and evens where your content for each post begins:
<?php if ($odd = $x%2){?>
<div class=”odd”>
<?php } else { ?><div class=”even”> <?php } ?>Make sure you close those div tags after your post content.
Then replace the endwhile in your loop with this:
<?php
$x++;
endwhile; ?>Now you can go CSS crazy changing the background, colors, sizes for each alternate post!”
Would that be correct?
The topic ‘2 column multi loop’ is closed to new replies.