$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pagination - How to determine if theres a next page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pagination - How to determine if theres a next page

matteradmin10PV0评论

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

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 edited Jul 16, 2018 at 16:49 wittich 1732 silver badges10 bronze badges asked Feb 12, 2011 at 11:56 Jiew MengJiew Meng 1,9115 gold badges24 silver badges40 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 20

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>';
  }
  echo '</ul>';
}

Hope This Helps

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

http://www.ericmmartin/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; ?>

the best solution is checking $wp_query->max_num_pages, but you also can use:

<?php
if(paginate_links()) {
...
}
Post a comment

comment list (0)

  1. No comments so far