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 new to wordpress development, just trying to convert my HTML into a WordPress theme, I started with Chris Coyer's blank theme.

<div class="navigation">
    <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>

How can I output the div only if there is a next_posts_link(). I need this as I will be using <ul> for my pagination. If I don't do that, I will get an empty bullet

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can use get_previous_posts_link and get_next_posts_link to determine if they exists like this:

$prev_link = get_previous_posts_link(__('&laquo; Older Entries'));
$next_link = get_next_posts_link(__('Newer Entries &raquo;'));
// as suggested in comments
if ($prev_link || $next_link) {
  echo '<ul class="navigation">';
  if ($prev_link){
    echo '<li>'.$prev_link .'</li>';
  }
  if ($next_link){
    echo '<li>'.$next_link .'</li>';
  }
}

Hope This Helps

share|improve this answer
    
Only problem there is you might potentially render an empty list, which will produce the invalid markup, and in effect will have the same issue the user has with the existing code.. –  t31os Feb 12 '11 at 12:38
    
@t31os, I guess I will do something like if ($prev_link || $next_link) // output ul –  Jiew Meng Feb 12 '11 at 13:16
    
Yeah that would do it.. ;) –  t31os Feb 12 '11 at 14:32
    
Updated the code –  Bainternet Feb 12 '11 at 17:49

I wrote this up a while ago, but should still be valid:

http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/

You can add the following function to your functions.php file:

/**
 * If more than one page exists, return TRUE.
 */
function show_posts_nav() {
    global $wp_query;
    return ($wp_query->max_num_pages > 1);
}

The update your code to:

<?php if (show_posts_nav()) : ?>
<div class="navigation">
    <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php endif; ?>
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.