找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1666|回复: 0
打印 上一主题 下一主题

WordPress主题中添加文章列表页页码导航的PHP代码实例

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-14 05:40:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            WordPress 默认给主题开发者的建议是在文章列表底部提供上下页按钮,所以没有提供直接用在文章列表下的分页导航的函数。这里我提供一个比较完善的分页导航函数。

/**
  *WordPress 文章列表分页导航
  *http://www.endskin.com/page-navi/
*/
function Bing_get_pagenavi( $query = false, $num = false, $before = '', $after = '', $options = array() ){
  global $wp_query;
  $options = wp_parse_args( $options, array(
    'pages_text' => '%CURRENT_PAGE%/%TOTAL_PAGES%',
    'current_text' => '%PAGE_NUMBER%',
    'page_text' => '%PAGE_NUMBER%',
    'first_text' => __( '« 首页', 'Bing' ),
    'last_text' => __( '尾页 »', 'Bing' ),
    'next_text' => __( '»', 'Bing' ),
    'prev_text' => '«',
    'dotright_text' => '...',
    'dotleft_text' => '...',
    'num_pages' => 5,
    'always_show' => 0,
    'num_larger_page_numbers' => 3,
    'larger_page_numbers_multiple' => 10
  ) );
  if( $wp_query->max_num_pages request;
    $numposts = $query->found_posts;
    $max_page = $query->max_num_pages;
    $posts_per_page = intval( $num );
  }else{
    $request = $wp_query->request;
    $numposts = $wp_query->found_posts;
    $max_page = $wp_query->max_num_pages;
    $posts_per_page = intval( get_query_var( 'posts_per_page' ) );
  }
  $paged = intval( get_query_var( 'paged' ) );
  if( empty( $paged ) || $paged == 0 ) $paged = 1;
  $pages_to_show = intval( $options['num_pages'] );
  $larger_page_to_show = intval( $options['num_larger_page_numbers'] );
  $larger_page_multiple = intval( $options['larger_page_numbers_multiple'] );
  $pages_to_show_minus_1 = $pages_to_show - 1;
  $half_page_start = floor( $pages_to_show_minus_1 / 2 );
  $half_page_end = ceil( $pages_to_show_minus_1 / 2 );
  $start_page = $paged - $half_page_start;
  if( $start_page  $max_page ){
    $start_page = $max_page - $pages_to_show_minus_1;
    $end_page = $max_page;
  }
  if( $start_page  $max_page ) $larger_start_page_end = $max_page;
  if( $larger_end_page_end > $max_page ) $larger_end_page_end = $max_page;
  if( $max_page > 1 || intval( $options['always_show'] ) == 1 ){
    $pages_text = str_replace( '%CURRENT_PAGE%', number_format_i18n( $paged ), $options['pages_text'] );
    $pages_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $max_page ), $pages_text);
    echo $before;
    if( !empty( $pages_text ) ) echo '' . $pages_text . '';
    if( $start_page >= 2 && $pages_to_show  0 && $larger_start_page_start > 0 && $larger_start_page_end ' . $current_page_text . '';
      }else{
        $page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
        echo '[url=' . esc_url( get_pagenum_link( $i ) ).']' . $page_text . '[/url]';
      }
    }
    if( empty( $query ) ) echo '';
    next_posts_link( $options['next_text'], $max_page );
    if( empty( $query ) ) echo '';
  }
  if( $larger_page_to_show > 0 && $larger_end_page_start
然后在需要使用分页导航的地方添加下边的代码:
            
            
您可能感兴趣的文章:
  • 编写PHP脚本使WordPress的主题支持Widget侧边栏
  • 实现WordPress主题侧边栏切换功能的PHP脚本详解
  • WordPress中给文章添加自定义字段及后台编辑功能区域
  • WordPress中使主题支持小工具以及添加插件启用函数
  • WordPress中编写自定义存储字段的相关PHP函数解析
  • WordPress的主题编写中获取头部模板和底部模板
  • WordPress主题制作之模板文件的引入方法
  • WordPress主题制作中自定义头部的相关PHP函数解析
  • 在WordPress中获取数据库字段内容和添加主题设置菜单
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端