0

I want to have my front page, with three columns, each assigned to a different category. Ideally I would like to query to fetch one post, per column, per page where possible, leaving the column empty if not.

My current loop simply gets 3 posts at a time. It is reasonable to assume that the 3 will not be one of each category.

Here is the loop:

<?php

            global $query_string;
            parse_str( $query_string, $my_query_array );
            $paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1;
            query_posts('post_type=post&posts_per_page=3&paged='.$paged);


            if ( have_posts() ) : while ( have_posts() ) : the_post();

            $category = choose_one_category(get_the_category());

            switch ($category){
                case "Festival News":
                    $left[] = $post;
                    break;
                case "Industry News":
                    $centre[] = $post;
                    break;
                case "Other":
                    $right[] = $post;
                    break;
            }


            endwhile; 
            ?>
            <div class="custom-pagination">

            <div ><?php previous_posts_link('&laquo; Previous') ?></div>

            <div ><?php next_posts_link('Next &raquo;') ?></div>
            </div>
            <?php endif;



            ?>
1

My recommendation would be to use a separate query for each column - getting 1 post per category - and pass the global $paged variable into each. This should exhibit the behavior you described in your question (at least it does on my site).

My setup

I have a top section that displays the 5 most recent posts from the "featured" category if you're on page 1 only. Then I have a sidebar that displays the first page of posts from each of 3 primary categories, each using their own query with posts_per_page=1. Then I have the main section that displays all posts normally with 5 posts on each page.

The first page returns my featured posts in a slider, the regular post section below it, and the first post from my three core categories in the sidebar. Page 2 hides the "featured" slider, returns the next 5 posts overall in the main section and the second post in each of my core categories. Page 3 and beyond do the same thing.

So what you'll need is not one loop over 3 categories, but 3 loops each over a single category and each using the global $paged variable.

  • so when a user goes to page 2, all three columns will update? – Mild Fuzz Oct 6 '10 at 14:44
  • If you're passing the global $paged variable into each query then yes, all three columns will update. – EAMann Oct 6 '10 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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