当前位置:首页 > CMS教程 > WordPress > 列表

wordpress实现文章分页功能例子

发布:smiling 来源: PHP粉丝网  添加日期:2015-10-16 11:00:15 浏览: 评论:0 

对于长的文章我们通常是需要进行分页才阅读的,但使用wordpress的朋友会发现wordpress文章并不具备分页功能了,那么如果要添加分页要如何实现呢?下面来看个例子.

之前大叔介绍过很多wordpress的分类列表分页和评论的分页,一直没介绍过文章内容的分页,今天有空写个教程来给大家学习一下,首先,wordpress文章分页要从编辑器和分页定义函数两个地方来实现,现在我们就直接走教程吧。

首先,将下面的代码放入wordpress主题文件夹的functions.php内:

  1. // 在 WordPress 编辑器添加“下一页”按钮 
  2. add_filter('mce_buttons','add_next_page_button'); 
  3. function add_next_page_button($mce_buttons) { 
  4.     $pos = array_search('wp_more',$mce_buttons,true); 
  5.     if ($pos !== false) { 
  6.         $tmp_buttons = array_slice($mce_buttons, 0, $pos+1); 
  7.         $tmp_buttons[] = 'wp_page'; 
  8.         $mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1)); 
  9.     } 
  10.     return $mce_buttons; 
  11. } 
  12. //内容分页 
  13. function custom_wp_link_pages( $args = '' ) { 
  14.     $defaults = array( 
  15.         'before' => '<div class="pagelist">分页阅读:', 
  16.         'after' => '</div>', 
  17.         'text_before' => '', 
  18.         'text_after' => '', 
  19.         'next_or_number' => 'number', 
  20.         'nextpagelink' =>'下一页', 
  21.         'previouspagelink' =>'上一页', 
  22.         'pagelink' => '%', 
  23.         'echo' => 1 
  24.     ); 
  25.     $r = wp_parse_args( $args, $defaults ); 
  26.     $r = apply_filters( 'wp_link_pages_args', $r ); 
  27.     extract( $r, EXTR_SKIP ); 
  28.     global $page, $numpages, $multipage, $more, $pagenow; 
  29.     $output = ''; 
  30.     if ( $multipage ) { 
  31.         if ( 'number' == $next_or_number ) { 
  32.             $output .= $before; 
  33.             for ( $i = 1; $i < ( $numpages + 1 ); $i = $i + 1 ) { 
  34.                 $j = str_replace( '%', $i, $pagelink ); 
  35.                 $output .= ' '; 
  36.                 if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) ) 
  37.                     $output .= _wp_link_page( $i ); 
  38.                 else 
  39.                     $output .= '<span>'; 
  40.                 $output .= $text_before . $j . $text_after; 
  41.                 if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) ) 
  42.                     $output .= '</a>'; 
  43.                 else 
  44.                     $output .= '</span>'; 
  45.             } 
  46.             $output .= $after; 
  47.         } else { 
  48.             if ( $more ) { 
  49.                 $output .= $before; 
  50.                 $i = $page - 1; 
  51.                 if ( $i && $more ) { 
  52.                     $output .= _wp_link_page( $i ); 
  53.                     $output .= $text_before . $previouspagelink . $text_after . '</a>'; 
  54.                 } 
  55.                 $i = $page + 1; 
  56.                 if ( $i <= $numpages && $more ) { 
  57.                     $output .= _wp_link_page( $i ); 
  58.                     $output .= $text_before . $nextpagelink . $text_after . '</a>'; 
  59.                 } //phpfensi.com 
  60.                 $output .= $after; 
  61.             } 
  62.         } 
  63.     } 
  64.     if ( $echo ) 
  65.         echo $output; 
  66.     return $output; 
  67. } 

给编辑器加了下一页按钮,也定义了分页函数,下面就是到single.php文章页面的相应位置里插入调用函数,即可前端显示分页按钮了,调用函数如下:

<?php custom_wp_link_pages();?>

到了这步,分页就出来了,CSS我也贴出吧,比较简约,如果你的css技术流弊的话,欢迎分享给大家.

  1. .pagelist { padding: 10px 0; background: #f3f3f3; text-align: center; margin-top: 20px } 
  2. .pagelist>span,.pagelist>a{background-color: #fff ;border: 1px#ddd solid ;color: #000;margin-left: 5px;padding: 4px 10px ;text-transform: uppercase; } 
  3. .pagelist>a:hover,.pagelist>span{background-color: #363636;color: #fff !important;}

Tags: wordpress文章分页 wordpress分页

分享到: