^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

wordpress文章页面分页地址伪静态

之前陌小雨分享过点击文章页面图片自动进入下一页的文章,这非常适合 wordpress 图片主题,比如 tob、xiu,但是有一个问题,就是分页地址没有伪静态,VIP群里面的小伙伴问我,如何使 wordpress 分页地址伪静态呢?类似于这种:https://dedewp.com/16994_1.html 这种。

网上有人分享过这种办法,陌小雨配合了之前分享的点击图片进入下一页的文章,最终代码就是这样子的:

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

//解析 url 的钩子
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
function add_custom_post_rewrite_rules($rules) {
$custom_rules = array('(\d+)_(\d+)\.html$' => 'index.phpp=$matches[1]&page=$matches[2]',);
$rules = array_merge($custom_rules, $rules);
return $rules;
}
//设置 url 钩子
add_filter('wp_link_pages_link', 'post_custom_rewrite_url');
function post_custom_rewrite_url($output){
$preg =  "/(.*)\/(\d+)\.html\/(\d+)/";
$output = preg_replace($preg, "$1/$2_$3.html", $output);
return $output;
}
//不许跳转
add_filter( 'redirect_canonical',  'post_custom_redirect_url');
function post_custom_redirect_url($output){
return false;
}
//获取图片进入下一页
//整理:https://dedewp.com
function nextpage(){
global $pages;
$link = get_permalink();
$max_page = count($pages);
if (get_query_var('page')) {
$pageno = get_query_var('page');
}
else{$pageno = '1';}
$next = $pageno+'1';
if ($pageno == $max_page) {
$nextpage = get_permalink(get_adjacent_post(true,'',true));
}
else{
$nextpage = $link.'/'.$next;
$nextpage = post_custom_rewrite_url($nextpage);
}
return $nextpage;
}
function img_info ($img_info){
$pattern = "/<img(.*)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*)alt=('|\")(.*)('|\")(.*)>/i";
$replacement = '<a title="点击图片查看下一张" href="'.nextpage().'"><img$1src=$2$3.$4$5 alt="'.get_the_title().'" $10></a>';
$img_info = preg_replace($pattern, $replacement, $img_info);
return $img_info;
}
add_filter('the_content', 'img_info');

上面的这种办法,适合固定链接为 www.dedewp.com/16994.html,但是对固定连接为/%category%/%post_id%.html 这样子的并不适合,所以陌小雨在上面代码的基础上,又优化了下,改良后的代码就是:

你的二维码名字
请输入验证码查看内容
验证码:
微信扫码,回复关键字“92wp”获取验证码即可查看

2018-11-5 日 演示网址已失效,去掉演示网址

赠人玫瑰,手有余香。