page_template toggle between 2 templates + 2 permalinks for same post

WordPress has a simpler function for what you’re trying to do, add_rewrite_endpoint. Additionally, the filter for a single post template is single_template, page_template fires on the page post type.

function wpd_detail_endpoint(){
    add_rewrite_endpoint( 'detail', EP_PERMALINK );
}
add_action( 'init', 'wpd_detail_endpoint' );

function wpd_detail_template( $template="" ) {
    global $wp_query;
    if( ! array_key_exists( 'detail', $wp_query->query_vars ) ) return $template;

    $template = locate_template( 'single-photos-detail.php' );
    return $template;
}
add_filter( 'single_template', 'wpd_detail_template' );

This assumes the template exists, you may want to do some additional checks as you’ve got in your original function.

Leave a Comment