WordPress网站去除URL中的Category?

Wordpress网站去除URL中的Category
WordPress网站去除URL中的Category

我们使用WordPress建立了网站后,还要进行SEO优化的相关工作,其中一项内容就是对网站的URL进行优化,以使WordPress网站的URL对搜索引擎更加友好。我们在另一篇文章中会讲到WordPress网站如何创建对SEO更加友好的URL。

我们通过WordPress后台“设置”-“固定链接”中的“自定义链接结构”,将创建了类似http://www.domain.com/category/category-name/post-name/这样的URL结构,其实当前的URL结构已经符合搜索引擎友好的URL了。但是很多像我一样有强迫症的同学,就是看着URL中的category不顺眼,想把它弄掉。

其实去掉URL中的“Category”很容易,只需要把下面代码复制到网站主题的function.php的文件中就可以用去掉这个惹人嫌的“category”了。function.php文件位于每个主题的根目录中,我们可以通过xshell对该文件进行编辑,也可以通过FTP访问文件目录,对该文件进行编辑。

function fix_slash( $string, $type )
{
global $wp_rewrite;
if ( $wp_rewrite->use_trailing_slashes == false )
{
    if ( $type != 'single' && $type != 'category' )
        return trailingslashit( $string );

    if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
        return trailingslashit( $string );

    if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
    {
        $aa_g = str_replace( "/category/", "/", $string );
        return trailingslashit( $aa_g );
    }
    if ( $type == 'category' )
        return trailingslashit( $string );
}
return $string;
}

add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );

这样我们就已经去除掉URL中的“Category”了。

作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/wordpress/remove-wordpress-url-category/

(0)
牛奇网牛奇网
上一篇 2021年2月27日 下午3:29
下一篇 2021年2月28日 下午12:08

发表回复

登录后才能评论
很多新手不知道如何选择外贸独立站主机,这里推荐一款使用量最大,性价比最高的国外独立站主机Hostinger,立即获取优惠