问题描述

我是新的 wordpress 开发,只是试图将我的 HTML 转换为 WordPress 主题,我开始与克里斯·科耶的空白主题。

<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>

如果有 next_posts_link(),我该如何输出。我需要这个,因为我将使用<ul> 作为我的分页。如果我不这样做,我会得到一个空的子弹

最佳解决方案

您可以使用 get_previous_posts_linkget_next_posts_link 来确定它们是否存在如下:

$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>';
}

希望这可以帮助

次佳解决方案

我写了一段时间以前,但仍然有效:

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

您可以将以下功能添加到 functions.php 文件中:

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

将您的代码更新为:

<?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; ?>

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。