Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

hi i got this code from a post, i want to split loop into 4 column

this is a archive page

i want to limit 1 post in first column 1 post in second column 4 posts in third column 5 post in fourth column

see i want to make like this http://demos.gabfirethemes.com/wp-newspaper/category/finance/ here are 3 column in archive page

please guide me - -

here are the code

<div class="First-column">
<?php while(have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) == 0 ) : // skip 'even' posts
$wp_query->next_post();
else :
?>
<?php the_post(); ?>

<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4></a>   

<?php endif; ?>
<?php endwhile; ?>
</div>

<?php $postcount = 0; rewind_posts(); ?>

<div class="Second-column">
<?php while(have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) != 0 ) : // skip 'odd' posts
$wp_query->next_post();
else :
?>
<?php the_post(); ?>

 <a href="<?php the_permalink(); ?>">
  <h4><?php the_title(); ?></h4></a>   

<?php endif; ?>
<?php endwhile; ?>
</div>

<?php $postcount = 0; rewind_posts(); ?>

<div class="third-column">
<?php while(have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) != 0 ) : // skip 'odd' posts
$wp_query->next_post();
else :
?>
<?php the_post(); ?>

<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4></a>   

<?php endif; ?>
<?php endwhile; ?>
</div>

<?php $postcount = 0; rewind_posts(); ?>

<div class="fourth-column">
<?php while(have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) != 0 ) : // skip 'odd' posts
$wp_query->next_post();
else :
?>
<?php the_post(); ?>

 <a href="<?php the_permalink(); ?>">
  <h4><?php the_title(); ?></h4></a>   

<?php endif; ?>
<?php endwhile; ?>
</div>
share|improve this question
3  
PHP/CSS Q. Off-topic. –  kaiser Oct 23 '11 at 13:23
    
possible duplicate of wordpress.stackexchange.com/questions/31781/… –  Michael Oct 23 '11 at 15:23

1 Answer 1

This was a lot more difficult than I anticipated. It seems having multiple while loops can confuse WordPress if it exhausts all the posts. In particular, if a while loop gets to the end of the posts, the next while loop starts from the beginning, causing duplicates to be shown.

To get round this we can use a (not very elegant) do_not_duplicate array to keep track of posts we have already displayed.

Here is my following solution. I've used a trick I've seen (using foreach) to make it flexible for creating more columns / altering the number of posts in each column. I hope the comments explain everything...

        <?php if ( have_posts() ) : ?>
            <?php
                //Can have as many loops as we like - set how many to appear in each loop.
               //In this example, 4 columns of length 1, 1, 4 and 5 posts respectively.
                $post_counts = array(1, 1,4, 5);
                foreach ($post_counts as $iteration => $max_count) {
                    $count = $max_count;  
                    /* Give our column specific id/class for styling */?>
                    <div id="column-<?php echo $iteration+1; ?> ">
                    //Loop inside the column
                    <?php while ( have_posts() and $count-- ) :
                            the_post(); 
                        /* Check if post has already been shown */
                        if (in_array($post->ID, $do_not_duplicate)) continue;
                            /* If not, add it to our do_not_duplicate array and show it */
                            $do_not_duplicate[] = $post->ID; ?>
                             <a href="<?php the_permalink() ?>" title="" ><?php the_title(); ?></a> </br>
                    <?php endwhile; ?>
                    </div>
            <?php }?>
        <?php else : ?>
            <p>No posts</p>
        <?php endif; ?>

This does the job, but there must be a neater way...?

share|improve this answer
    
Multiple loops can use both wp_reset_query and/or rewind_post , also another technique is to use array_chunk. –  Wyck Oct 23 '11 at 15:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.