是的,这是可能的,但想想WebElaine在评论中写了什么。
您需要一个查询变量(例如。jobname
) 存储的名称jobs
自定义帖子类型
您还需要一个重写规则来解释/jobs/{post-slug}/contact
地址为“联系人”页面并保留{post-slug}
in查询变量jobname
.
add_filter( \'query_vars\', \'se354723_query_vars\' );
add_action( \'init\', \'se354723_rewrite_job_contact\' );
function se354723_query_vars( $vars )
{
$vars[] = "jobname";
return $vars;
}
function se354723_rewrite_job_contact()
{
$cpt_slug = \'jobs\';
$target_page = \'contact-page-slug\';
add_rewrite_rule(
"$cpt_slug/([^/]+)/contact/?$",
\'index.php?pagename=\'. $target_page .\'&post_type=page&jobname=$matches[1]\',
\'top\'
);
}
您可以检查新查询变量的值(
jobname
) 在“联系人”页面上
get_query_var()
作用
$jobID = 0;
$jobname = get_query_var(\'jobname\', false);
if ( !empty($jobname) )
{
$jobname = sanitize_title_for_query( $jobname );
$arg = [
\'post_type\' => \'jobs\',
\'name\' => $jobname,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
];
$result = get_posts( $arg );
if ( is_array($result) && !empty($result) )
$jobID = (int)$result[0];
}