剥离父类别,但在Wordpress永久链接中保留子类别

时间:2019-06-27 04:03:54

标签: php wordpress url-rewriting

我有一个网址结构类似

的网站

example.com/news/story-1
example.com/news/local/story-2
example.com/food/tacos/story-3

现在,它已被移动到一个多站点,其中/ news /和/ food /作为站点的子文件夹。

在这些子站点中,顶级/默认类别分别是“新闻”或“食物”,替换为“未分类”。因此,如果永久链接设置为%category%/%postname%/,则永久链接显示为

example.com/news/news/local/story-title
example.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-2
example.com/food/tacos/story-3

以及浏览类别(如

)的功能

example.com/news/
example.com/news/local/
example.com/news/local/crime/

然而,由于其网址现在位于顶级类别中,所以现在的帖子为404(这是可以预期的)

example.com/news/story-1

(这是我想要的,但是我不想将整个站点设置为%postname%/,因为您丢失了简单的树链接类别导航)。

从我收集的信息中,我认为我需要使用pre_post_link或重写规则来将这些顶级帖子视为我的永久链接结构只是%postname%/而不是%category%/%postname%/,但这是在那里我被卡住了。

我尝试过的内容:我尝试过一些浮动的解决方案,除其他外,我尝试了一些变体,但顶部类别的帖子链接仍然剥夺了404 wordpress.stackexchange.com/a/133587/170714

0 个答案:

没有答案
相关问题