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、主题前端分页调用标签用
2、固定连接设置为 域名/%category%/%post_id%.html
3、下方代码添加到 主题 function.php文件中 (代码添加完成后,再后台设置中重新保存固定连接)
//分类 单页 tag 加html 伪静态
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
$wp_rewrite->extra_permastructs['post_tag']['struct'] = 'tag/%post_tag%-1.html';
$wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%-1.html';
}
add_action( 'init', 'custom_page_rules' );
//重定义分页连接 伪静态
add_filter('rewrite_rules_array','add_rewrite_rules');
function add_rewrite_rules($aRules){
$aNewRules = array(
'(.+?)-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
);
$aNewRules1 = array(
'tag/(.+?)-(\d+).html$' => 'index.php?tag=$matches[1]&paged=$matches[2]',
);
$aRules = $aNewRules + $aRules;
$aRules = $aNewRules1 + $aRules;
return $aRules;
}
add_filter( 'redirect_canonical', 'post_custom_redirect_url');
function post_custom_redirect_url($output){
return false;
}
function native_pagenavi(){
global $wp_query, $wp_rewrite;
$wp_query->query_vars["paged"]; //当前页数
$wp_query->query_vars["category_name"]; //当前分类名
$category = '/'.$wp_query->query["category_name"];
if($wp_query->query["category_name"] == ""){
$tag = $wp_query->query_vars["tag"];
$category = '/tag/'.$tag;
}
$wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
$pagination = array(
"base" => "",
"format" => "",
"total" => $wp_query->max_num_pages, //最大页数
"current" => $current,
"prev_text" => "« ",
"next_text" => " »"
);
$pagination["base"] =$category."-%#%.html";
if( !empty($wp_query->query_vars["s"]) ) {
$pagination["add_args"] = array("s"=>get_query_var("s"));
}
print_R($pagination);
echo paginate_links($pagination);
}
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、主题前端分页调用标签用
2.2、固定连接设置为 域名/%category%/%post_id%.html
2.3、function.php文件中添加如下代码来实现使用ID做为 标签分类的链接
add_action('generate_rewrite_rules','tag_rewrite_rules');
add_filter('term_link','tag_term_link',10,3);
add_action('query_vars', 'tag_query_vars');
function tag_rewrite_rules($wp_rewrite){
$new_rules = array(
'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true',
'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function tag_term_link($link,$term,$taxonomy){
if($taxonomy=='post_tag'){
return home_url('/tag/'.$term->term_id.'-1.html');
}
return $link;
}
function tag_query_vars($public_query_vars){
$public_query_vars[] = 'tag_id';
return $public_query_vars;
}
2.2、在function.php添加如如下代码
//分类 单页 tag 加html 伪静态
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
$wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%-1.html';
}
add_action( 'init', 'custom_page_rules' );
//重定义分页连接 伪静态
add_filter('rewrite_rules_array','add_rewrite_rules');
function add_rewrite_rules($aRules){
$aNewRules = array(
'(.+?)-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
);
$aNewRules1 = array(
'tag/(\d+)-(\d+).html$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
);
$aRules = $aNewRules + $aRules;
$aRules = $aNewRules1 + $aRules;
return $aRules;
}
//禁止访问原连接
add_filter( 'redirect_canonical', 'post_custom_redirect_url');
function post_custom_redirect_url($output){
return false;
}
function native_pagenavi(){
global $wp_query, $wp_rewrite;
$category = '/'.$wp_query->query["category_name"];
if($wp_query->query["category_name"] == ""){ //如果分类名获取为空,则获取标签ID 或标签名
$tag = $wp_query->query_vars["tag_id"];
$category = '/tag/'.$tag;
}
$wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
$pagination = array(
"base" => "",
"format" => "",
"total" => $wp_query->max_num_pages, //最大页数
"current" => $current,
"prev_text" => "« ",
"next_text" => " »"
);
$pagination["base"] = $category."-%#%.html"; //分页连接后加html
if( !empty($wp_query->query_vars["s"]) ) {
$pagination["add_args"] = array("s"=>get_query_var("s"));
}
echo paginate_links($pagination);
}
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、主题前端分页调用标签用
3.2、固定连接设置为 域名/%category%/%post_id%.html
3.3、function.php文件中添加如下代码来实现使用ID做为 标签分类的链接
add_action('generate_rewrite_rules','tag_rewrite_rules');
add_filter('term_link','tag_term_link',10,3);
add_action('query_vars', 'tag_query_vars');
function tag_rewrite_rules($wp_rewrite){
$new_rules = array(
'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true',
'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function tag_term_link($link,$term,$taxonomy){
if($taxonomy=='post_tag'){
return home_url('/tag/'.$term->term_id.'/page-1.html');
}
return $link;
}
function tag_query_vars($public_query_vars){
$public_query_vars[] = 'tag_id';
return $public_query_vars;
}
//分类 单页 tag 加html 伪静态
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html';
$wp_rewrite -> extra_permastructs['category']['struct'] = '/%category%/page-1.html';
}
add_action( 'init', 'custom_page_rules' );
//重定义分页连接 伪静态
add_filter('rewrite_rules_array','add_rewrite_rules');
function add_rewrite_rules($aRules){
$aNewRules = array(
'(.+?)/page-([0-9]{1,}).html$' =>'index.php?category_name=$matches[1]&paged=$matches[2]',
);
$aNewRules1 = array(
'tag/(\d+)/page-(\d+).html$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
);
$aRules = $aNewRules + $aRules;
$aRules = $aNewRules1 + $aRules;
return $aRules;
}
//禁止访问原连接
add_filter( 'redirect_canonical', 'post_custom_redirect_url');
function post_custom_redirect_url($output){
return false;
}
function native_pagenavi(){
global $wp_query, $wp_rewrite;
$category = '/'.$wp_query->query["category_name"];
if($wp_query->query["category_name"] == ""){ //如果分类名获取为空,则获取标签ID 或标签名
$tag = $wp_query->query_vars["tag_id"];
$category = '/tag/'.$tag;
}
$wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1;
$pagination = array(
"base" => "",
"format" => "",
"total" => $wp_query->max_num_pages, //最大页数
"current" => $current,
"prev_text" => "« ",
"next_text" => " »"
);
$pagination["base"] = $category."/page-%#%.html"; //分页连接后加html
if( !empty($wp_query->query_vars["s"]) ) {
$pagination["add_args"] = array("s"=>get_query_var("s"));
}
echo paginate_links($pagination);
}
最后说明:这三种方法大同小异,具体也就是连接的层级关系,和使用id或标签名做连接的区别,分类列表页默认为分页的第一页,这样可以避免页面重复,造成权重分散,更利于SEO优化。