Php 在Wordpress permalink中剥离父类别但保留子类别
我有一个网址结构像 example.com/news/story-1Php 在Wordpress permalink中剥离父类别但保留子类别,php,wordpress,url-rewriting,Php,Wordpress,Url Rewriting,我有一个网址结构像 example.com/news/story-1 example.com/news/local/story-2 example.com/food/tacos/story-3 现在它被移动到一个多站点,站点子文件夹为/news/和/food/ 在这些子网站中,顶级/默认类别分别为新闻或食品,取代未分类。因此,如果permalink设置为%category%/%postname%/,则permalink显示为 example.com/news/news/local/story t
example.com/news/local/story-2
example.com/food/tacos/story-3 现在它被移动到一个多站点,站点子文件夹为/news/和/food/ 在这些子网站中,顶级/默认类别分别为新闻或食品,取代未分类。因此,如果permalink设置为
%category%/%postname%/
,则permalink显示为
example.com/news/news/local/story titleexample.com/food/food/tacos/story-title 为了删除与站点子文件夹重复的类别,我在functions.php中添加了以下内容:
add_filter( 'post_link', 'remove_parent_category', 10, 3 );
function remove_parent_category( $permalink, $post, $leavename ) {
$permalink_array = explode("/", $permalink);
$clean_permalink = array_unique($permalink_array);
$new_permalink = implode("/", $clean_permalink);
return $new_permalink;
}
这管用,还给我
example.com/news/local/story-2example.com/food/tacos/story-3 以及浏览类别的能力,如 example.com/news/
example.com/news/local/
example.com/news/local/crime/
等等 然而,顶级类别的帖子现在是404(这是意料之中的),因为它们的url现在是404 example.com/news/story-1 (这正是我想要的,但我不想将整个站点设置为
%postname%/
,因为您会丢失简单的树链接类别导航)
从我收集的内容来看,我认为我需要使用pre_post_链接或重写规则来处理那些顶级帖子,就好像我的永久链接结构只是%postname%/
,而不是%category%/%postname%/
,但这就是我被卡住的地方
我尝试过的内容:我尝试过一些解决方案,其中包括我尝试过的一种变体,但与顶级类别的帖子链接仍然被剥离404如果顶级类别已经是新闻和食品,为什么需要为这些帖子设置一个类别?意思是,从你的描述中,你说你基本上把新闻和食物作为重复的类别。在这种情况下,您不需要为这些帖子设置类别,只需使用默认设置即可。然后将permalink结构设置为just use/postname.Correct,“News”和“Food”只是默认类别的方便名称-这就是为什么我一直在搜索如何从permalink中删除“uncategorized”的答案,但对其他类别使用category/postname-这本质上是相同的问题。我不想使用just/postname,因为我喜欢在permalink中有子cat,如果有的话。在这种情况下,我认为最好的做法是创建端点。@HoseinShurabi你能扩展一下吗?有两个文档可以指导你。如果你有任何问题,请告诉我。