Need to display same custom post type on 2 different singles templates
I have a client who wants to display home plans (a custom post type) on their website in 2 differently branded sections, depending on which listing page the post is accessed from. I found this solution from @gmazzap from a few years ago, and it looks promising, but I can’t make it work: Multiple templates for custom post type
Specifically, it fails to modify the URL on the alternate listing page: it simply uses the slug specified in the CPT registration:
'rewrite' => array( 'slug' => 'plan', 'with_front' => false ),
Which means when I click a plan link on ‘page_kioskoverview,php’ template it displays the post on the standard ‘single-plans.php’ template rather than the alternate ‘single-kioskplans.php’ template.
Here’s what my code looks like:
add_action('template_redirect', 'change_plans_plink');
function change_plans_plink() {
if (is_page_template('page_kioskoverview.php')) {
add_filter( 'post_link', 'plans_query_string', 10, 2 );
}
}
function plans_query_string( $url, $post ) {
if ( $post->post_type === 'plans' ) {
$url = add_query_arg( array('style'=>'alt'), $url );
}
return $url;
}
//designate alternative single template for above alt link
add_action('template_include', 'kiosk_plan_single');
function kiosk_plan_single($template) {
if( is_singular('plans') ) {
$alt = filter_input(INPUT_GET, 'style', FILTER_SANITIZE_STRING);
if ( $alt === 'alt' ) $template = 'single-kioskplans.php';
}
return $template;
}
After a few days looking at it (and trying some variations without success), I cannot spot the problem, but I must be missing something.
Leave an answer
You must login or register to add a new answer .