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 have at the end of my blog page an element that shows / lists the next articles from page 2.

If there is no second page , this element shows nothing.

I would like to hide the element automatically, as long when there is no second page.

Once there is a second page, the item should be displayed automatically.

Does anyone know how I can implement something like that?

here is a screenshot of this element when the blog have two sites: enter image description here

and here, when an second page does not exist: enter image description here

You'll see, this looks not nice when an second page does not exist.

I have implemented this function with the following code:

                <?php if ( is_home() && !is_paged() ) {
              $post_num = get_option('posts_per_page'); ?>
              <div class="next-content">
                <div class="page-header"><h3 class="article">Further articles<a href="<?php echo esc_url( home_url( '/' ) ); ?>page/2/" title="Zur Seite 2">on page 2</a>:</h3></div>
                <ul>
                <?php query_posts('showposts='.$post_num.'&offset='.$post_num);
                if (have_posts()) : ?><?php while (have_posts()) : the_post();  ?>
                  <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
                <?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?>
                </ul>
              </div>
            <?php } ?>

I hope you can help me! :>

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can use 2 properties of the global WP_Query: max_num_pages and paged.

After that avoid using query_posts for the new query use a new instance of WP_Query or get_posts.

global $wp_query;
if ($wp_query->max_num_pages > 1 && $wp_query->get('paged') < $wp_query->max_num_pages ) {
  $next_page = (int)$wp_query->get('paged') + 1;
  ?>
  <div class="next-content">
    <div class="page-header"><h3 class="article">Weiter <a href="<?php echo esc_url( home_url( '/' ) ); ?>page/<?php echo $next_page; ?>/" title="Zur Seite <?php echo $next_page; ?>">auf Seite <?php echo $next_page; ?></a> mit:</h3></div>
    <?php
    $post_num = $wp_query->get('posts_per_page') ? : get_option('posts_per_page');
    $next_posts = get_posts('posts_per_page=' . $post_num . '&paged=' . $next_page);
    if ( ! empty($next_posts) ) {
    echo '<ul>';
    foreach( $next_posts as $next_post) {
    ?>
    <li><a href="<?php echo get_permalink($next_post) ?>" rel="bookmark"><?php echo get_the_title($next_post); ?></a></li>
    <?php
    }
    echo '</ul>';
    }
    ?>
  </div>
  <?php
} 
share|improve this answer
    
hazelnut, my solution is very similar to the one posted by @s_ha_dum. He beat me by 2 minutes... but I had written all answer when got notified of his answer so I've posted it. The only difference is that my solution works also for other pages than 2: on 2nd page it preview the 3rd page posts and so on. If you don't need this feature mark s_ha_dum answer as accepted. –  G. M. Sep 15 '13 at 23:16
    
i have tried it and put the code into the have_posts block and it do nothing. :< I have put it after the ":" in <?php if ( ! have_posts() ) : ?> –  hazelnut Sep 15 '13 at 23:34
    
@hazelnut if you mean the <?php if ( ! have_posts() ) : ?> block in your code, you don't need it at all. My code is the only thing you need. Just use it after closing the main loop. –  G. M. Sep 16 '13 at 7:34
    
i don't get it. I get an White Page of Death. Look at my index.php pastebin.com/QsNq9Lq7 have i forgot something? –  hazelnut Sep 16 '13 at 7:59
    
Ah, i got it! Sry i was a little bit tired and I'm a newbie on PHP. :-) Thanks for you solution! –  hazelnut Sep 16 '13 at 8:55

Just move all of the markup into the have_posts block.

if ( is_home() && !is_paged() ) {
  $post_num = get_option('posts_per_page'); 
  $next = new WP_Query('posts_per_page='.$post_num.'&offset='.$post_num); 
  if ($next->have_posts()) {
    while ($next->have_posts()) { 
      $next->the_post(); ?>
      <div class="next-content">
        <div class="page-header"><h3 class="article">Further articles<a href="<?php echo esc_url( home_url( '/' ) ); ?>page/2/" title="Zur Seite 2">on page 2</a>:</h3></div>
        <ul>
          <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        </ul>
      </div><?php 
    }
  }
  wp_reset_postdata();
}

Notes:

  1. You should not be using query_posts so I changed it.
  2. showposts is deprecated and has been for a very long time now, so I changed it.
  3. "Alternative" control structure syntax it nothing but confusing, hard to read, and prone to error, so I changed it.
  4. Since you haven't clobbered the wp_query global with query_posts you don't have to reset the whole query. wp_reset_postdata is good enough.
share|improve this answer

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.