如何将永久链接设置为“ /%postname%/”的URL删除“类别”

问题描述 投票:0回答:1

我希望菜单链接显示这些类别,但URL中没有'类别'一词。例如,代替site.com/category/slow /site.com/category/fast /,它显示site.com/slow /site.com/fast /。这些类别下的所有帖子都将显示为site.com/postname /

使用经过几天在线搜索后设法找到的PHP代码,它确实有助于从URL中删除单词“类别”,但是我必须将永久链接设置为“ /%category%/%postname%/以使其正常工作。如果将其设置为“ /%postname%/”,则site.com/slow /site.com/fast /将返回404(找不到页面)错误。

function remove_category( $string, $type )
{ 
if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'category' ) !== false ) ) {
     $url_without_category = str_replace( "/category/", "/", $string );
            return trailingslashit( $url_without_category );
}
return $string;
}
add_filter( 'user_trailingslashit', 'remove_category', 100, 2);

将永久链接设置为“ /%category%/%postname%/”,我的所有帖子将变为site.com/slow/postname /site.com/fast/postname /,其中不是我想要的。

任何人都知道如何将永久链接设置保持为“ /%postname%/”,同时能够从URL中删除单词“ category”,以便所有帖子的结构均为site.com/postname /site.com/slow /site.com/fast /下,并且site.com/slow /site.com/fast /正常工作。感谢你的帮助。谢谢。

php wordpress categories permalinks
1个回答
0
投票

重写您的.htaccess文件

RewriteRule ^ /([[a-zA-Z0-9-/] +)$ category / file_name.php?value = $ 1

© www.soinside.com 2019 - 2024. All rights reserved.