如果有人遇到这样一个“奇怪”的需求,这里有一个解决方案;)此外,当发布作业CPT(或您想要关联的任何CPT)时,您需要将公司CPT的id(其slug将用于形成永久链接的其他部分)保存为作业的post meta。
<?php
add_filter(\'init\', \'add_page_rewrite_rules\');
function add_page_rewrite_rules(){
global $wp_rewrite, $wp;
add_rewrite_rule(\'^company/([^/]+)/job/([^/]+)\', \'index.php?company=$matches[1]&job=$matches[2]\', \'top\');
$job_structure = \'/job/%job%\';
$wp_rewrite->add_rewrite_tag("%job%", \'([^/]+)\', "job=");
$wp_rewrite->add_permastruct(\'job\', $job_structure, false);
}
add_filter(\'post_type_link\', \'job_permalink\', 10, 3);
function job_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
\'%job%\',
\'job\'
);
if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
job_link = \'\';
if ( strpos($permalink, \'job\') !== false ) {
$company_id = get_post_meta($post->ID, \'job_company_id\', true);
$company = basename(get_permalink($company_id));
$job_link = \'company\'.\'/\'.$company;
}
$rewritereplace = array(
$post->post_name,
$job_link.\'/job\'
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
else {
// if they\'re not using the fancy permalink option
}
return $permalink;
}
?>
希望这对别人有帮助!如果在执行此操作时遇到问题,请与我联系。