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've done some searches here and on Google but everything I'm finding isn't working correctly. I was hoping to post my code here and get help with where I'm going wrong.

On the index page of my site, I have one "featured" post that has a different layout than the rest of the posts. When you click to the next page, that featured post goes away and leaves me with an uneven count of posts and I need an even post count. Here's the code I found and have tried implementing with no joy.

I'm using the following query (everything else is the same at the count code and below):

<?php
  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts("paged=$page&posts_per_page=10"); 
?>

This is the code I want to be able to use:

<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$page_num = $paged;
    if ($pagenum='') $pagenum = 1;
    if ($pagenum > 1) { $post_num = 10 } else { $post_num = 9 }
query_posts('showposts='.$post_num.'&paged='.$page_num); `
?>

This would be the same for the rest of the page:

<?php $count = 1; ?>

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

<?php if ((!is_paged()) && ($count == 1 )){  ?>

 // FEATURED HTML HERE

<?php } else {  ?>

 // REGULAR HTML HERE

<?php } $count++; ?>

<?php endwhile; ?>

// PAGINATION HERE
share|improve this question

2 Answers 2

You should use the pre_get_posts filter. You can exclude the homepage with ! is_front_page or ! is_home depending on your configuration.

/**
 * Changes the number of posts per page if not is_home
 *
 * @author  SFNdesign, Curtis McHale
 */
function wptt_change_posts_on_page( $query ) {
    if ( ! is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_action( 'pre_get_posts', 'wptt_change_posts_on_page' );

As I said, you may need to use ! is_front_page depending on how your theme is set up. Here is a good blog post explaining more on those conditionals.

http://wpthemetutorial.com/2011/12/12/clearing-up-confusion-about-is_home-and-is_front_page/

Oh and never use query_posts, evar. https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

share|improve this answer
    
+1 for pre_get_posts, but you'll still need to account for paged, since is_home() still returns TRUE on paged pages. –  Chip Bennett Nov 27 '12 at 16:47
    
Thanks, I've discovered that neither yours or Johannes options work. Either wreck pagination. I'll have to come up with some other solution. –  Justice Is Cheap Nov 28 '12 at 15:37

The logic you came up with in the second snippet makes sense. Nonetheless, there are a couple of things wrong with your code.

For one, the $pagenum variable is redundant and you do not need it. For another, you assign the return value of get_query_var() to $page and then in the following lines use $paged - you want that to be the same. The following should do:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_num = ($paged > 1) ? 10 : 9;
// query for posts

Further, in the markup snippet (the 3rd one of your question), $count is not needed, you can simply use $paged as well. As an aside and for the sake of completeness, I would not employ so many php opening and closing tags, but that is a matter of taste and will not influence the functionality of your code.

One more: Unless this is your main loop, you should use the WP_Query class instead of query_posts. And if it is, the usage of query_posts is not as wrong, but it still would be recommendable you'd check out the pre_get_posts filter instead.

share|improve this answer
    
This comes close to working correctly but when I page, it drops one post from the list on each page. –  Justice Is Cheap Nov 28 '12 at 15:41

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.