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.

I am having a few issues with a site I am building, on the news page It has 1 featured post from the news category and then under that more news category posts, but missing out the featured news item as I don't want that item repeating.

for example

page 1

  • featured post - post 1
  • post 2
  • post 3
  • post 4
  • post 5

page 2

  • post 6
  • post 7
  • post 8
  • post 9

and so on...

So far I have had quite a few different issues created by different methods of resolving the problem. I have had the last post from page 1 repeating on page 2, I have had pagination break, thinking on page 1 it has 3 pages and then on every page after it says there are 5 pages and then it just gets messy from there.

This is the code I have currently, It isn't working, if anybody can help me with this that would be fantastic. Basically what I need is my news category list to have for example 5 posts on the first page with one of those posts being featured (and in a separate row from the rest of the posts, they are contained in one row ) and all other pages have 4 posts and for it all to paginate properly and have no duplicate posts.

Hopefully this enough to go on.

(the numbers for posts per page in the code block are just for test purposes, dont fancy making lots of test posts)

<?php get_header(); ?>

<?php global $wp_query;  

$total_pages = $wp_query->max_num_pages;  

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

?>


    <?php if(is_paged()) : ?>
        <section class="content">

            <div class="news-list">
            <div class="row">

            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

                <article class="col span_6 news-item">
                    <?php the_post_thumbnail(); ?>
                    <h3><?php the_title(); ?></h3>
                    <p><?php echo limit_words(get_the_excerpt(), '25'); ?>...</p>
                    <p class="date"><?php echo get_the_date(); ?></p>
                    <span class="readmore"><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, 'block_link', true); ?>Full story</a> »</span>
                </article>


            <?php endwhile; 
                if ($total_pages > 1){  

                $current_page = max(1, get_query_var('paged'));  

                echo '<div class="page_nav">'; 

                echo paginate_links(array(  
                  'base' => get_pagenum_link(1) . '%_%',  
                  'format' => 'page/%#%',  
                  'current' => $current_page,  
                  'total' => $total_pages,  
                ));  

                echo '</div>'; 

                }  ?>
            <?php endif; ?>

            </div>
            </div>

        </section>


    <?php else : ?>

        <section class="content">

            <?php query_posts('showposts=1'); ?>

            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

                <section class="feature-post">
                <div class="row">

                    <div class="col span_6">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <div class="col span_3">
                        <h3><?php the_title(); ?></h3>
                        <p><?php echo limit_words(get_the_excerpt(), '25'); ?>...</p>
                        <p class="date"><?php echo get_the_date(); ?></p>
                        <span class="readmore"><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, 'block_link', true); ?>Full story</a> »</span>
                    </div>
                    <div class="col span_3">

                    </div>

                </div>
                </section>


            <?php endwhile; ?>

            <?php endif; wp_reset_query(); ?>

        </section>

        <div class="news-list">
            <div class="row">
            <?php global $query_string; ?> 
            <?php query_posts( $query_string . '&offset=1' ); ?>
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

                <article class="col span_6 news-item">
                    <?php the_post_thumbnail(); ?>
                    <h3><?php the_title(); ?></h3>
                    <p><?php echo limit_words(get_the_excerpt(), '25'); ?>...</p>
                    <p class="date"><?php echo get_the_date(); ?></p>
                    <span class="readmore"><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, 'block_link', true); ?>Full story</a> »</span>
                </article>


            <?php endwhile; 
                if ($total_pages > 1){  

                $current_page = max(1, get_query_var('paged'));  

                echo '<div class="page_nav">'; 

                echo paginate_links(array(  
                  'base' => get_pagenum_link(1) . '%_%',  
                  'format' => 'page/%#%',  
                  'current' => $current_page,  
                  'total' => $total_pages,  
                ));  

                echo '</div>'; 

                }  ?>
            <?php endif; ?>

            </div>
            </div>

    <?php endif; ?>

<?php get_footer(); ?>

I get the following using this code

Page 1

  • featured post - post 1
  • post 2
  • post 3
  • post 4
  • post 5

page 2

  • post 5
  • post 6
  • post 7
  • post 8

page 3

  • post 9
  • post 10

...and so on, After page 2, the pagination is working properly and there is no repeated posts :S

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Well first of all, we can get rid of all the query_posts business in the template. There's always a better method than using query_posts.

If the primary goal is to have 5 posts on the first page and 4 on all subsequent pages, we can do that very easily before the query happens via a function in the theme's functions.php hooked to the the pre_get_posts action.

This basically says "If it's the home page and the main query, check the page number- if it's less than page 2, load 5 posts, otherwise load 4."

function wpa_featured_posts_per_page( $query ){
    if( $query->is_home() && $query->is_main_query() ){
        if( 2 > $query->query_vars['paged'] ){
            $query->set( 'posts_per_page', 5 );
        } else {
            $query->set( 'posts_per_page', 4 );
        }
    }
}
add_action( 'pre_get_posts', 'wpa_featured_posts_per_page' );

As for styling the first post differently, you can check the global $wp_query->current_post within the loop and output different markup if it equals zero or greater than zero (current_post is zero-indexed, it starts at zero and goes up from there).

You can also run the same loop more than once by calling rewind_posts(); to reset the internal post counter.

share|improve this answer
    
Fantastic! thanks for that, I will try it tomorrow, thanks for the help its much appreciated –  user30944 Apr 3 '13 at 21:49
    
I have tried this and have gotten much further with the situation, I have the formatting i wanted and no repeating posts, the only thing left that isn't behaving the way I want it too is the pagination. the first page thinks theirs 3 pages because it is set to 3 posts per page and then when you go to page two it thinks theirs 5 pages because on pages after 1 posts per page is 2. do you know a way to fix this, if so that would be fantastic –  user30944 Apr 4 '13 at 15:26
add comment

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.