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.
Related Posts:
- For custom templates, is it better to use `template_include` or `type_template`?
- How To Load an HTML File As A WordPress Page (With No 301/No Redirect)
- WordPress archive index page
- How To make Custom page of every Category
- Why does a custom php code inside a custom template not get executed?
- Displaying Child Page’s Information
- Single page template
- How to *remove* a parent theme page template from a child theme?
- How to Change 404 page title
- How to add a new product type on woocommerce product types? [closed]
- Updating WordPress overwriting customization tweaks – strategies?
- How to customize the default HTML for WordPress Attachments
- Is it possible to manipulate the list of page templates?
- How to redirect WordPress home page to custom static HTML page
- wp_trim_words() does not trim the_content() in WordPress
- How can I create an alternative home page?
- Templates & CSS – Proper Programming Practice?
- index.php template is used instead of blog page
- Make page template that is just an “include” of another?
- Custom php file in wordpress
- Selecting An image from the Media LIbrary on the frontend
- How to apply a custom template for posts by default?
- How do I deque the default stylesheet?
- WordPress Template not affecting the working, keeps defaulting to the index.php template
- Recommend an open source, template driven website builder UI that produces a working WordPress site [closed]
- Woocommerce Product page edit
- Custom page template not working
- Create default_content layout template based on post id
- Remove template drop down for certain pages
- How do I add a new template to my site
- How to customize core admin pages within a theme?
- Is there a way to set different post templates for parent posts and child posts in the same post type?
- How can I make site-wide content editable, for example footer content
- Query post for ‘selected category’ in archive.php
- Showcase elementor templates
- How to get custom page template being edited in gutenberg editor
- need help with a user-specific custom page template
- Prepopulating a specific page template with HTML/CSS
- Make custom page look exactly as homepage
- Best practice – pages – ACF or inline coding
- All top-level pages are using index.php as template
- Set Title from Custom Template
- Move default page templates to sub directory
- Custom templates vs page-slug
- Page attributes missing
- How to hook up search results page to a back-end page?
- List top 5 authors with most posts
- Selected template is not respected for home page in WordPress
- Is there a way to by-pass the pagination function on one custom template?
- Single-page.php template file name not shown
- How to create custom search page
- Editing a post template
- How to get the current category with custom posts
- How to display custom post type?
- How to add custom ‘layout’ buttons to wordpress editor?
- How to create custom layouts and static pages in themes for clients
- Custom Content Page
- Create a Page template selector page
- Creating custom page template
- How to create a custom WordPress front page
- Force WordPress to load a specific template
- Custom Same Page Search
- change top banner content on specific page
- Organize template parts and page templates in folders in regards of template hierarchy
- How to set up conditionals in page templates?
- Dynamic Sidebars based on page name
- templates with page teasers Or Featured Items
- Translate custom template with WPML
- Remove Sidebars from page
- WordPress hierarchy clarification, does post_type_archive take precedence over taxonomy-term archive?
- Product page not found if product data is set Appointment Service
- Conflict between Yoast SEO and my custom theme
- Page with some post value id showing page not found
- How to add a template – so that i can choose a different menu for some pages
- How can I delete the word `Category:` from page titles? [duplicate]
- How can I add category variable inside posts?
- How to use the same custom page template to iframe multiple urls with entry of new url entered from backend
- How can I make custom page templates work on child theme?
- Create page when a new site in multisite network is created?
- wordpress custom posts page not showing
- How do I target this page specifically?
- Still desperate about multiple TinyMCEs in widgets – is there any good solution to this yet?
- Use another theme template in my theme
- Redirect Every Instance of The Author Template Pages to Custom Author Pages
- Page Templates Used in Custom Post Type
- Taxonomy template by post type
- Passing query variables to a custom page template
- Best way to create a page outside of the pages list
- Adding pages to the Admin panel
- Make Terms Under Custom Taxonomy Use Specific Single.php Template
- Custom single page based on which custom page template is selected
- Setting Custom WordPress “Posts page” AS “Front page”
- Page template with custom html fields inside a content
- Adding page Title in a custom coded page
- Adding css and js to a blank page created with custom template
- Modifying a local template
- Custom template not working for existing pages [closed]
- use another theme for just one page
- How to apply styles to the custom page template
- Create a page for each database table entries