或者

WordPress实现全站.html链接

作者:小鸡炖蘑菇 浏览:114 发布时间:2017-07-24
分享 评论 0

大家一般都知道通过后台自定义固定连接,可以实现文章链接以.html结尾,相比大家也发现了一个小小的现象,wordpress后台设置的自定义固定链接却没有办法在页面上实现,今天南京SEO就告诉大家一个好方法实现全部页面以.html结尾。


add_action('init', 'html_page_permalink', -1);

register_activation_hook(__FILE__, 'barley_active');

register_deactivation_hook(__FILE__, 'barley_deactive');

function html_page_permalink() {

    global $wp_rewrite;

    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){

        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';

    } 

}

add_filter('user_trailingslashit', 'no_page_slash',66,2);

function no_page_slash($string, $type){

    global $wp_rewrite;

    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){

        return untrailingslashit($string);

    } else {

        return $string;

    }

}


function barley_active() {

    global $wp_rewrite;

    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){

        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';

    }

    $wp_rewrite->flush_rules();

}

function barley_deactive() {

    global $wp_rewrite;

    $wp_rewrite->page_structure = str_replace(".html","",$wp_rewrite->page_structure);

    $wp_rewrite->flush_rules();

}

  将这段代码放在主题文件的functions.php里面,再去后台设置固定链接为为文章名.html或者是id.html,再更新一次,去前台看看是不是实现了你想要的效果呢!