I’m trying to use this page_template template from the WP forums, with the same aim as the original author:
I am trying to use two separate permalinks/templates for a single post
– each providing different views of the post (through different templates).
http://example.com/category/post-name -> uses template1.php
http://example.com/category/post-name/details -> uses template2.php
I’ve modified the code according to the changes discussed in the thread, but my alternative template (for a custom post type) isn’t being loaded — /post-name/detail just redirects to /post-name/.
Any ideas?
// Add 'detail' to the permalink structure / query string
function detail_rewrite_rules( $rules ) {
$newrules = array();
$newrules['(.+?)/([^/]+)(/[0-9]+)?/detail/?$'] = 'index.php?category_name=$matches[1]&name=$matches[2]&page=$matches[3]&detail=1';
return $newrules + $rules;
}
add_filter( 'rewrite_rules_array','detail_rewrite_rules' );
function detail_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(.+?)/([^/]+)(/[0-9]+)?/detail/?$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_action( 'init','detail_flush_rules' );
// Open up 'detail'
function detail_queryvars( $qvars ) {
$qvars[] = 'detail';
return $qvars;
}
add_filter('query_vars', 'detail_queryvars' );
// Switch between templates
function filter_single_template($template){
$object = get_queried_object();
$templates = array();
/* If detail is in the query string, create a list of templates to use */
// if($_GET['detail']) {
// if( get_query_var('detail') ) {
global $wp_query;
if ( $wp_query->query_vars['detail'] ) {
echo "<script>console.log('Detail template activated');</script>";
$templates[] = "single-photos-detail.php";
// $templates[] = "detail-single.php";
// $templates[] = "detail-single-{$object->post_type}.php";
}
/* If one of our custom detail templates exists, return it.
* Otherwise return the original template
*/
$templateSwitch = locate_template($templates);
return (!empty($templateSwitch)) ? locate_template($templates) : $template;
}
add_filter('page_template', 'filter_single_template');
WordPress has a simpler function for what you’re trying to do,
add_rewrite_endpoint
. Additionally, the filter for a single post template issingle_template
,page_template
fires on thepage
post type.This assumes the template exists, you may want to do some additional checks as you’ve got in your original function.