我有三种不同的自定义帖子类型:
公司员工项目通过ACF关系字段连接,因此公司可以有多个员工,每个员工都可以附加一个项目。函数的作用是:检索附加的post对象,以创建某种关系。
我需要为这些CPT创建三个permalink结构:
对于公司:www.somepage.com/%company_name%对于员工:www.somepage.com/%company_name%/%employee_name%对于项目:www.somepage.com/%company_name%/%employee_name%/%project_name%我已经设法使用以下代码为它们设置永久链接结构:
function dd_custom_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if(($post->post_type != \'project\' && $post->post_type != \'employee\' && $post->post_type != \'company\') || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))
return $permalink;
if (get_field("employees", $post->ID)) {
$permalink = str_replace(\'%company%\', $post->post_name, $permalink);
} elseif (get_field("company", $post->ID)) {
$permalink = str_replace(\'%company%\', get_field("company", $post->ID)[0]->post_name, $permalink);
$permalink = str_replace(\'%employee%\', $post->post_name, $permalink);
} elseif (get_field("employee", $post->ID)) {
$employee= get_field("employee", $post->ID)[0];
$company= get_field("company", $employee->ID)[0];
$permalink = str_replace(\'%company%\', $company->post_name, $permalink);
$permalink = str_replace(\'%employee%\', $employee->post_name, $permalink);
$permalink = str_replace(\'%project%\', $post->post_name, $permalink);
}
return $permalink;
}
add_filter(\'post_type_link\', \'dd_custom_permalinks\', 10, 3);
问题是我找不到使它们有效的方法,因为该结构总是指向Company(自定义url结构的第一部分)。也许我必须以某种方式将进一步与CPT查询绑定?
我也尝试过使用自定义重写规则,但我不明白如何做到公平。以下是我到目前为止得到的结果(没有工作):
function dd_custom_rewrite_rules() {
add_rewrite_tag(\'%company%\', \'([^/]+)\', \'company=\');
add_permastruct(\'company\', \'/%company%\', false);
add_rewrite_rule(\'^company/([^/]+)/([^/]+)/?\',\'index.php?company=$matches[2]\',\'top\');
add_rewrite_tag(\'%employee%\', \'([^/]+)\', \'employee=\');
add_permastruct(\'employee\', \'/%company%/%employee%\', false);
add_rewrite_rule(\'^employee/([^/]+)/([^/]+)/?\',\'index.php?employee=$matches[2]\',\'top\');
add_rewrite_tag(\'%project%\', \'([^/]+)\', \'project=\');
add_permastruct(\'project\', \'/%company%/%employee%/%project%\', false);
add_rewrite_rule(\'^project/([^/]+)/([^/]+)/?\',\'index.php?project=$matches[2]\',\'top\');
}
add_action( \'init\', \'dd_custom_rewrite_rules\' );
有什么想法吗?我如何(或者是否)让它正常工作?
最合适的回答,由SO网友:Damian Doman 整理而成
Sally CJ评论了一个重要的细节,帮助我达到了我所需要的。
除此之外,我还在与似乎是regex错误的问题作斗争——我的一条规则凌驾于另一条规则之上,因为没有$
用于结束字符串的正则表达式中的字符。
自定义重写的工作解决方案:
function dd_custom_rewrite_rules() {
add_rewrite_tag(\'%company%\', \'([a-z0-9-]+)\', \'company=\');
add_rewrite_tag(\'%employee%\', \'([a-z0-9-]+)\', \'employee=\');
add_rewrite_tag(\'%project%\', \'([a-z0-9-]+)\', \'project=\');
add_permastruct(\'project\', \'/company/%company%/%employee%/%project%\', false);
add_permastruct(\'employee\', \'/company/%company%/%employee%\', false);
add_permastruct(\'company\', \'/company/%company%\', false);
add_rewrite_rule(\'^company[/]%company%[/]?\',\'index.php?company=$matches[1]\',\'top\');
add_rewrite_rule(\'^company[/]([a-z0-9-]+)[/]([a-z0-9-]+)[/]?$\',\'index.php?employee=$matches[2]\',\'top\');
add_rewrite_rule(\'^company[/]([a-z0-9-]+)[/]([a-z0-9-]+)[/]([a-z0-9-]+)[/]?$\',\'index.php?project=$matches[3]\',\'top\');
}