首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在子分类页面中包含父分类固定链接

问在子分类页面中包含父分类固定链接
EN

Stack Overflow用户
提问于 2020-07-14 11:51:08
回答 1查看 174关注 0票数 0

老实说,我已经迷失了方向,我需要一些指导来完成我一直在做的一个项目

我有一个单一的层次分类法,county,我在我的两个自定义帖子类型上使用它,floorplan和ready-made。

我正在尝试设置指向自定义分类页面的固定链接,这些页面将显示为:

{post-type}/{parent-tax}/{child-tax}

现在,您可以导航到或,因此floorplans/{parent-tax}和floorplans/{child-tax}将指向相同的county-taxonomy.php模板。您可以对现成/{父税}和现成/{子税}执行相同的操作。

但是,如果直接浏览分类法,并导航到子分类法,则url将显示为county/{parent-tax}/{ child -tax}。

我不明白的是,当您通过自定义post类型库导航时,如何将父分类附加到子分类固定链接。floorplans/{parent-tax}/{child-tax}和ready-made/{parent-tax}/{child-tax}是404。

我需要写一个重写规则吗?我有点不知所措,不知道怎么做。为了让我的父分类法和子分类法显示在帖子类型的固定链接中,我严格遵循了this guide,但难题的最后一部分是能够导航到特定于每种帖子类型的子分类法页面,并在该url中包含父分类法。

我的重写规则现在看起来与我链接的指南几乎相同,

代码语言:javascript
复制
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
    $newRules  = array();
    $newRules['floorplan/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[2]'; // my custom structure will always have the post name as the 5th uri segment
    $newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[2]'; // my custom structure will always have the post name as the 5th uri segment

    $newRules['floorplan/(.+)/(.+)/'] = 'index.php?county=$matches[1]'; // my custom structure will always have the post name as the 5th uri segment
    $newRules['ready-made/(.+)/(.+)/'] = 'index.php?county=$matches[1]'; // my custom structure will always have the post name as the 5th uri segment

    $newRules['floorplan/(.+)/?$']                = 'index.php?county=$matches[1]';
    $newRules['ready-made/(.+)/?$']                = 'index.php?county=$matches[1]';
    return array_merge($newRules, $rules);
}

我添加了这两个中间值,认为这将针对父母税/孩子税的永久链接结构,后面没有任何内容,但我不确定如何从那里拼凑起来。任何建议都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-16 00:05:52

回答我自己的问题,

为了达到预期的效果,下面是最终的重写函数:

代码语言:javascript
复制
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
    $newRules  = array();
    $newRules['floorplan/(.+)/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[3]';
    $newRules['ready-made/(.+)/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[3]';

    $newRules['floorplan/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
    $newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';

    $newRules['floorplan/(.+)/?$']                = 'index.php?county=$matches[1]';
    $newRules['ready-made/(.+)/?$']                = 'index.php?county=$matches[1]';
    return array_merge($newRules, $rules);
}

我从其他stackoverflow答案中拼凑了出来,但据我所知,这些重写本质上是在说:对于给定的url,显示post类型/分类类型/等,其中matches[x]是您试图显示的内容的实际页面标题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62887791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档