Hide featured post on second page

I have a index.php with a featured post and a number of other posts and the standard wp_pagenavi at the bottom.

I’m using the 2 loops from the wordpress codex like this:

Read More
<?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
  <!-- Do stuff... -->
<?php endwhile; ?>
  <!-- Do other stuff... -->
<?php if (have_posts()) : while (have_posts()) : the_post(); 
if( $post->ID == $do_not_duplicate ) continue; ?>
 <!-- Do stuff... -->
<?php endwhile; endif; ?>

If I’ve read the theme hierarchy correctly, an archive.php page should be used when clicking “Previous” in the pagination section… (correct?)

However, this is not the case, clicking previous is showing me a page with the same layout as my index.php and thus with the same featured post. (pagination on the other posts does work though)

I’ve been messing around with is_paged but haven’t been able to get it working…
Thanks for the help!

Related posts

Leave a Reply

4 comments

  1. In short no – the template used is based on the query, and when paginating you are essentially repeating the same query, but for a different page. In general the template will be the same.

    archive.php can be used for most queries, but often preferable templates exist (e.g. category templates, tag templates, author templates etc.). What is preferable is determined by the template hierarchy. For the home page,however, index.php is often used, and the template will persist with pagination.

    As for displaying only something on ‘page 1’, you can try the following:

    <?php 
     //initialize $do_not_duplicate
     $do_not_duplicate=0;
     //is_paged returns true if we are on page 2,3,...
     if(!is_paged()):
          //Get featured content for page 1
          $my_query = new WP_Query('category_name=featured&posts_per_page=1');
          while ($my_query->have_posts()) : $my_query->the_post();
               $do_not_duplicate = $post->ID;
               //Do stuff...
          endwhile;
     endif;
    
     //Display rest of content
     if (have_posts()) : 
        while (have_posts()) : the_post(); 
             if( $post->ID == $do_not_duplicate ) continue;
             //Do stuff
        endwhile; 
      endif; 
     ?>
    

    Not tested

  2. is_paged is just a boolean function for if the list has multiple pages or not.

    What you need is: $wp_query->query_vars['paged']. Surround your featured post code with,

    global $wp_query;
    if ( !isset($wp_query->query_vars['paged']) || $wp_query->query_vars['paged'] == 1 ) :
    # Featured post code
    endif;
    

    I think that’ll do the trick for you.

  3. Easy: WP got a lot on board for this. No direct array/object access needed.

    // Before the loop
    $paged = get_query_var( 'paged' );
    
    // In the loop
    if ( 
        ( ! isset( $paged )  OR ! $paged )
        AND is_sticky( get_the_ID() )
    {
        // do stuff
    }