WordPress全站伪静态加html后缀

2023年10 月30日 / 网站源码 / 没有评论 / 4,672次

WordPress全站伪静态,分别在分类列表页、列表分页、TAG标签、单页、文章页后加html后缀

WordPress全站伪静态方法一

效果展示
分类伪静态 www.域名.com/分类名-1.html
分页伪静态 www.域名.com/分类名-2.html
单页伪静态 www.域名.com/about.html
文章页伪静态 www.域名.com/分类名/127348.html
tag标签伪静态 www.域名.com/tag/标签名-1.html
分页伪静态 www.域名.com/tag/标签名-2.html
使用
1、主题前端分页调用标签用 <? native_pagenavi() ?>
2、固定连接设置为 域名/%category%/%post_id%.html
3、下方代码添加到 主题 function.php文件中 (代码添加完成后,再后台设置中重新保存固定连接)

  1. //分类 单页 tag 加html 伪静态
  2. function custom_page_rules() {
  3.     global $wp_rewrite;
  4.     $wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
  5.     $wp_rewrite->extra_permastructs['post_tag']['struct'] = 'tag/%post_tag%-1.html';
  6.     $wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%-1.html';
  7. }
  8. add_action( 'init', 'custom_page_rules' );
  9. //重定义分页连接 伪静态
  10. add_filter('rewrite_rules_array','add_rewrite_rules');
  11. function add_rewrite_rules($aRules){
  12.        $aNewRules = array(
  13.         '(.+?)-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
  14.         );
  15.         $aNewRules1 = array(
  16.             'tag/(.+?)-(\d+).html$'   => 'index.php?tag=$matches[1]&paged=$matches[2]',
  17.         );
  18.         $aRules = $aNewRules + $aRules;
  19.         $aRules = $aNewRules1 + $aRules;
  20.         return $aRules;
  21. }
  22. add_filter( 'redirect_canonical', 'post_custom_redirect_url');
  23. function post_custom_redirect_url($output){
  24.     return false;
  25. }
  26. function native_pagenavi(){
  27.  global $wp_query$wp_rewrite;
  28.  $wp_query->query_vars["paged"];         //当前页数
  29.  $wp_query->query_vars["category_name"]; //当前分类名
  30.  $category = '/'.$wp_query->query["category_name"];
  31.  if($wp_query->query["category_name"] == ""){
  32.      $tag = $wp_query->query_vars["tag"];
  33.       $category = '/tag/'.$tag;
  34.  }
  35.  $wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
  36.   $pagination = array(
  37.       "base"       => "",
  38.       "format"     => "",
  39.       "total"       => $wp_query->max_num_pages, //最大页数
  40.       "current"      => $current,
  41.       "prev_text"  => "« ",
  42.       "next_text"  => " »"
  43.    );
  44.  $pagination["base"] =$category."-%#%.html";
  45.  if( !emptyempty($wp_query->query_vars["s"]) ) {
  46.        $pagination["add_args"] = array("s"=>get_query_var("s"));
  47.    }
  48.   print_R($pagination);
  49.    echo paginate_links($pagination);
  50.  }

WordPress全站伪静态方法二、

与方法一类似,不同的是标签使用ID做为 标签分类的链接。
效果展示
分类伪静态 www.域名.com/分类名-1.html
分页伪静态 www.域名.com/分类名-2.html
单页伪静态 www.域名.com/about.html
文章页伪静态 www.域名.com/分类名/127348.html
tag标签伪静态 www.域名.com/tag/标签ID-1.html
分页伪静态 www.域名.com/tag/标签ID-2.html
2.1、主题前端分页调用标签用 <? native_pagenavi() ?>
2.2、固定连接设置为 域名/%category%/%post_id%.html
2.3、function.php文件中添加如下代码来实现使用ID做为 标签分类的链接

  1. add_action('generate_rewrite_rules','tag_rewrite_rules');
  2.     add_filter('term_link','tag_term_link',10,3);
  3.     add_action('query_vars', 'tag_query_vars');
  4.     function tag_rewrite_rules($wp_rewrite){
  5.     $new_rules = array(
  6.     'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  7.     'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  8.     'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true',
  9.     'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  10.     'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]',
  11.     );
  12.     $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  13.     }
  14.     function tag_term_link($link,$term,$taxonomy){
  15.     if($taxonomy=='post_tag'){
  16.     return home_url('/tag/'.$term->term_id.'-1.html');
  17.     }
  18.     return $link;
  19.     }
  20.     function tag_query_vars($public_query_vars){
  21.     $public_query_vars[] = 'tag_id';
  22.     return $public_query_vars;
  23.     }

2.2、在function.php添加如如下代码

  1. //分类 单页 tag 加html 伪静态
  2. function custom_page_rules() {
  3.     global $wp_rewrite;
  4.     $wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
  5.     $wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%-1.html';
  6. }
  7. add_action( 'init', 'custom_page_rules' );
  8. //重定义分页连接 伪静态
  9. add_filter('rewrite_rules_array','add_rewrite_rules');
  10. function add_rewrite_rules($aRules){
  11.          $aNewRules = array(
  12.             '(.+?)-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
  13.         );
  14.         $aNewRules1 = array(
  15.             'tag/(\d+)-(\d+).html$'   => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  16.         );
  17.         $aRules = $aNewRules + $aRules;
  18.         $aRules = $aNewRules1 + $aRules;
  19.         return $aRules;
  20. }
  21. //禁止访问原连接
  22. add_filter( 'redirect_canonical', 'post_custom_redirect_url');
  23. function post_custom_redirect_url($output){
  24.     return false;
  25. }
  26. function native_pagenavi(){
  27.  global $wp_query$wp_rewrite;
  28.  $category = '/'.$wp_query->query["category_name"];
  29.  if($wp_query->query["category_name"] == ""){ //如果分类名获取为空,则获取标签ID 或标签名
  30.      $tag = $wp_query->query_vars["tag_id"];
  31.       $category = '/tag/'.$tag;
  32.  }
  33.  $wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
  34.   $pagination = array(
  35.       "base"       => "",
  36.       "format"     => "",
  37.       "total"       => $wp_query->max_num_pages, //最大页数
  38.       "current"      => $current,
  39.       "prev_text"  => "« ",
  40.       "next_text"  => " »"
  41.    );
  42.    $pagination["base"] = $category."-%#%.html"//分页连接后加html
  43.    if( !emptyempty($wp_query->query_vars["s"]) ) {
  44.        $pagination["add_args"] = array("s"=>get_query_var("s"));
  45.    }
  46.    echo paginate_links($pagination);
  47. }

WordPress全站伪静态方法三、

采用分类层级连接。
效果展示
分类伪静态 www.域名.com/分类名/page-1.html
分页伪静态 www.域名.com/分类名/page-2.html
单页伪静态 www.域名.com/about.html
文章页伪静态 www.域名.com/分类名/子分类/127348.html
tag标签伪静态 www.域名.com/tag/标签ID/page-1.html
分页伪静态 www.域名.com/tag/标签ID/page-2.html
3.1、主题前端分页调用标签用 <? native_pagenavi() ?>
3.2、固定连接设置为 域名/%category%/%post_id%.html
3.3、function.php文件中添加如下代码来实现使用ID做为 标签分类的链接

  1. add_action('generate_rewrite_rules','tag_rewrite_rules');
  2.     add_filter('term_link','tag_term_link',10,3);
  3.     add_action('query_vars', 'tag_query_vars');
  4.     function tag_rewrite_rules($wp_rewrite){
  5.     $new_rules = array(
  6.     'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  7.     'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  8.     'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true',
  9.     'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  10.     'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]',
  11.     );
  12.     $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  13.     }
  14.     function tag_term_link($link,$term,$taxonomy){
  15.     if($taxonomy=='post_tag'){
  16.     return home_url('/tag/'.$term->term_id.'/page-1.html');
  17.     }
  18.     return $link;
  19.     }
  20.     function tag_query_vars($public_query_vars){
  21.     $public_query_vars[] = 'tag_id';
  22.     return $public_query_vars;
  23.     }
  1. //分类 单页 tag 加html 伪静态
  2. function custom_page_rules() {
  3.     global $wp_rewrite;
  4.     $wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
  5.     $wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%/page-1.html';
  6. }
  7. add_action( 'init', 'custom_page_rules' );
  8. //重定义分页连接 伪静态
  9. add_filter('rewrite_rules_array','add_rewrite_rules');
  10. function add_rewrite_rules($aRules){
  11.          $aNewRules = array(
  12.             '(.+?)/page-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
  13.         );
  14.         $aNewRules1 = array(
  15.             'tag/(\d+)/page-(\d+).html$'   => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  16.         );
  17.         $aRules = $aNewRules + $aRules;
  18.         $aRules = $aNewRules1 + $aRules;
  19.         return $aRules;
  20. }
  21. //禁止访问原连接
  22. add_filter( 'redirect_canonical', 'post_custom_redirect_url');
  23. function post_custom_redirect_url($output){
  24.     return false;
  25. }
  26. function native_pagenavi(){
  27.  global $wp_query$wp_rewrite;
  28.  $category = '/'.$wp_query->query["category_name"];
  29.  if($wp_query->query["category_name"] == ""){ //如果分类名获取为空,则获取标签ID 或标签名
  30.      $tag = $wp_query->query_vars["tag_id"];
  31.       $category = '/tag/'.$tag;
  32.  }
  33.  $wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
  34.   $pagination = array(
  35.       "base"       => "",
  36.       "format"     => "",
  37.       "total"       => $wp_query->max_num_pages, //最大页数
  38.       "current"      => $current,
  39.       "prev_text"  => "« ",
  40.       "next_text"  => " »"
  41.    );
  42.    $pagination["base"] = $category."/page-%#%.html"//分页连接后加html
  43.    if( !emptyempty($wp_query->query_vars["s"]) ) {
  44.        $pagination["add_args"] = array("s"=>get_query_var("s"));
  45.    }
  46.    echo paginate_links($pagination);
  47. }

最后说明:这三种方法大同小异,具体也就是连接的层级关系,和使用id或标签名做连接的区别,分类列表页默认为分页的第一页,这样可以避免页面重复,造成权重分散,更利于SEO优化。