WordPress 无插件实现分页导航

  1. 首先找到wordpress的主题文件夹Themes,里面有一个functions.php循环函数文件打开,在末尾 ?> 符号前加入以下代码:

    //pagenavi
    function wp_nav( $p = 2 )
    {
      if ( is_singular() ) return;
      global $wp_query, $paged;
      $max_page = $wp_query->max_num_pages;
      if ( $max_page == 1 ) return;
      if ( empty( $paged ) ) $paged = 1;
      echo '<span class="page-numbers">' . $paged . ' / ' . $max_page . ' </span> ';
      if ( $paged > 1 ) p_link( $paged - 1, __('&laquo; Previous'),__('&laquo; Previous') );
      if ( $paged > $p + 1 ) p_link( 1, 'First page' );
      if ( $paged > $p + 2 ) echo '<span class="page-numbers">...</span>';
      for( $i = $paged - $p;  $i <= $paged + $p; $i++ ) {
      if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
      }
      if ( $paged < $max_page - $p - 1 ) echo '<span class="page-numbers">...</span>';
      if ( $paged < $max_page - $p ) p_link( $max_page, 'Last page' );
      if ( $paged < $max_page ) p_link( $paged + 1, __('Next &raquo;'), __('Next &raquo;') );
    }
    function p_link( $i, $title = '', $linktype = '' )
    {
      if ( $title == '' ) $title = "The {$i} page";
      if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
      echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a> ";
    }

    1

  2. 2
  3. 在主题样式表文件style.css中加入关联样式代码:

    /**分页导航**/.wp_nav{padding:20px 0;text-align:center;font-size: 13px;line-height:18px;}.wp_nav .page-numbers,.wp_nav .dots:hover,.wp_nav .pages:hover{border: 1px solid #eee;text-decoration: none;background: #5080D8;color: #fff;text-shadow: 0px -1px rgba(0, 0, 0, 0.2);display:inline-block;margin:2px;padding:1px;padding: 4px 9px;}.wp_nav .current,.wp_nav .current:hover,.wp_nav a.page-numbers:hover{text-decoration: none;background: #f9f9f9;color: #5080D8;}.wp_nav .pages,.wp_nav .pages:hover{vertical-align: middle;color:#CCC;width:120px;}

    3

  4. 添加调用代码至主题的index.php、archive.php、category.php、search.php的对应位置,调用代码为:

    <div class="wp_nav"><?php echo wp_nav(); ?></div>

    4

  5. 保存回到网站刷新看一下效果达成。

    5

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

− 8 = 1