php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构:

http://www.xyqzmt.cn/1.html/2

通常固定链接都是这样的结构,设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题,最后放弃挣扎,如果遇到很长的文章全都放在一个页面里面。

95736fc0487abd81535ff7854580ea61.png

今天在知更鸟大佬的博客上找到了结局办法。http://zmingcx.com/pseudo-static-article-page-links.html

将下面代码添加到当前主题 functions.php中:

本文提供一下/%post_id%.html的修改方法。

// 适合/%post_id%.html分页链接修正

class Rewrite_Inner_Page_Links_id{

var $separator;

function __construct(){

$this->separator = ‘/page-‘;

if( !is_admin() || defined( ‘DOING_AJAX’ ) ) :

add_filter( ‘wp_link_pages_link’, array( $this, ‘inner_page_link_format’ ), 10, 2 );

add_filter( ‘get_comments_pagenum_link’, array( $this, ‘comment_page_link_format’ ) );

add_filter( ‘redirect_canonical’, array( $this, ‘cancel_redirect_for_paged_posts’ ), 10, 2 );

endif;

if( is_admin() ) :

add_filter( ‘rewrite_rules_array’, array( $this, ‘pagelink_rewrite_rules’ ) );

register_activation_hook( __FILE__, array( $this, ‘flush_rewrite_rules’ ) ) ;

register_deactivation_hook( __FILE__, array( $this, ‘flush_rewrite_rules’ ) );

endif;

}

function flush_rewrite_rules(){

flush_rewrite_rules();

}

// 修改post分页链接的格式

function inner_page_link_format( $link, $number ){

if( $number > 1 ){

if( preg_match( ‘%

$link = preg_replace( “%(\.html)/(\d*)%”, $this->separator.”$2$1″, $link );

}

}

return $link;

}

// 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录

function pagelink_rewrite_rules( $rules ){

foreach ($rules as $rule => $rewrite) {

if ( $rule == ‘([0-9]+).html(/[0-9]+)?/?$’ ) {

unset($rules[$rule]);

}

}

$new_rule[‘([0-9]+)(‘.$this->separator.'([0-9]+))?.html/?$’] = ‘index.php?p=$matches[1]&page=$matches[3]’;

return $new_rule + $rules;

}

// 禁止WordPress将页面分页链接跳转到原来的格式

function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){

global $wp_query;

if( is_single() && $wp_query->get( ‘page’ ) > 1 ){

return false;

}

return true;

}

}

new Rewrite_Inner_Page_Links_id();

提示:添加代码后,需要保存一下固定链接设置。

之后再次打开文章分页链接,会变成类似的:

/morning-paper-news/page-2.html

/132/page-2.html

解决了我的千古难题,我弄的那个漫画教程网站,复制贴吧里面的教程,有大量的图片,起码要分个七八页才OK的。增加网站的PV也挺好的!前提就是内容足够吸引人!等有机会再研究那种能整页展示分页的功能。(参考太平洋电脑网上的那种)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值