admin管理员组文章数量:1410697
Let's say I have a post type called jobs
, and a page called contact
with a form in it. Now I want the contact page to show on every job url ending with /contact
. So job-1/contact
, job2/contact
etc.
I want the same contact-page to display but with access to the current job id.
Is this possible?
Let's say I have a post type called jobs
, and a page called contact
with a form in it. Now I want the contact page to show on every job url ending with /contact
. So job-1/contact
, job2/contact
etc.
I want the same contact-page to display but with access to the current job id.
Is this possible?
Share Improve this question edited Dec 16, 2019 at 23:39 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Dec 16, 2019 at 18:37 CintaCinta 101 1- This may be difficult to achieve, and it's definitely not best practice for SEO, as search engines will see all of these as duplicates. Why do you need to make the contact page look like a child of multiple jobs? Perhaps there is another approach to the underlying reason that could be solved another way. – WebElaine Commented Dec 16, 2019 at 22:12
1 Answer
Reset to default 0Yes, it's possible, but think about what WebElaine wrote in comment.
You'll need a query variable (e.g. jobname
) to store the name of jobs
custom post type.
You also need a rewrite rule that interprets /jobs/{post-slug}/contact
address as a "contact" page
and keep {post-slug}
in query variable 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'
);
}
You can check value of the new query variable (jobname
) on the "contact" page
with the get_query_var()
function.
$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];
}
本文标签: url rewritingHow to get relative page for every post post1contact post2contact
版权声明:本文标题:url rewriting - How to get relative page for every post: post-1contact. post-2contact 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744898419a2631202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论