WordPress页面链接URL添加.html后缀

无论是否开启伪静态,默认功能中wordpress的单页面url链接都不能设置包括 .html 在内的各种后缀,即使手动编辑固定链接添加“.html”也会被转码为“-html”形式,但是也并非完全不能设置,因为可以通过编辑函数代码或者直接使用wordpress插件实现,且比较简单,下面是提取自“.html on PAGES”插件的函数代码。

操作步骤:

1、把下面的代码添加在主题的 functions.php 文件:

add_action('init', 'html_page_permalink', -1);
function html_page_permalink() {
	global $wp_rewrite;
	if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
		$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
	}
}

2、保存文件后,进入后台设置——固定链接中,重新保存一次设置即可生效。

3、代码生效后,wordpress页面链接自动带上 .html 后缀,原链接会失效,这样可以避免网站同一页面存在两个链接影响SEO优化。

PS:.html on PAGES插件是款很老的插件,虽然长年没更新,但是提取的代码可以正常使用。