I'm working on a site that uses two different pages to filter lists of posts associated with the same custom post type ("media"), and both pages have their own templates-- a more general page named page-media.php and one that's more narrowly focused via taxonomy, page-music-library.php. While both of these pages filter lists of posts of the "media" custom post type, I would like each to render the posts in a different single- view when the post titles are clicked. For the main page-media.php template, I created a single-media.php template for single post views associated with that page. However, I'm not quite sure how to define an alternate single- view associated with the page-music-library.php template?

I'm seeing from this post and elsewhere that as of WP 4.7, "Post Type Templates" can offer more flexibility in this way, but I'm not clear on whether that feature would apply to my situation. Thanks for any insight here, and please let me know if my objective is unclear in any way.

2 Answers 2


Well one approach would be to create a generic single-media.php file and put a condition inside it which checks for the specific taxonomy. Then you would be able to include the other two based on the condition.

So, create the main template - single-media.php and the other two templates page-media.php and page-music-library.php. Then you could check for the the specific taxonomy in this file and include the corresponding file based on the condition.

a simple example of single-media.php would be

Template Name: Single Media
Template Post Type: post, media 

//header and stuff

if ( has_term( $term, $taxonomy, $post ) ) {

    include '{path}/page-music-library.php';
else {
    include '{path}/page-media.php' ;
  • thanks for the response- this seems like a good approach. Is there a way, however, to distinguish the view based on the page from which it originated? Thus, if viewing a single post via page-media.php, it would display one single view, and if viewing via page-music-library.php it would display an alternate single view?
    – nickpish
    Commented Jul 20, 2017 at 3:22
  • Well if you just want to open that particular template when someone clicks on the title, then you could simply redirect the user using the wp_redirect($url) function.
    – Digvijayad
    Commented Jul 20, 2017 at 3:32
  • @nickpish take a look at this tutorial as well, it uses the add_rewrite_rule to redirect a page template based on that.
    – Digvijayad
    Commented Jul 20, 2017 at 11:02

You could add a rewrite endpoint, which can be appended onto the end of single media permalinks to identify which requests should get the alternate template.

Step 1, add the endpoint:

function wpd_rewrite_endpoint() {
    add_rewrite_endpoint( 'music-library', EP_PERMALINK );
add_action( 'init', 'wpd_rewrite_endpoint' );

After adding the above code to a plugin or your theme's functions.php file, visit the Settings > Permalinks page to flush rewrite rules.

Assuming your permalink for a media post is:


You should now also be able to visit the URL with endpoint and see the same view:


Step 2, add a single_template filter:

function wpd_single_template( $template ) {
    global $wp_query;
    // if this is a media post
    // AND the media-library endpoint is present,
    // load the alternate template
    if( 'media' == get_post_type( get_queried_object() )
        && isset( $wp_query->query_vars['music-library'] ) ){
            $template = locate_template( 'single-music-library.php' );
    return $template;
add_filter( 'single_template', 'wpd_single_template' );

After adding the above code to your plugin or theme's functions.php, the original URL will continue to load your single-media.php template, while any URLs containing the endpoint will load the single-music-library.php template.

In both cases, the canonical URL will point to the original URL, so only that single version will be indexed by search engines, avoiding duplicate content issues.

The last step will be to modify your page-music-library.php template to add music-library/ after the permalinks for each media post.

  • thank you for the thorough answer- I didn't realize this would be as involved. If we pare this down a bit and I simply wanted to display a different return link on the single- post page depending on how one reached that post-- i.e. "Return to Media Library" (page-media.php) or "Return to Music Library" (page-music-library.php) is this possible with a simple if/else statement in the single- page? Basically just guiding them back from whence they came...
    – nickpish
    Commented Jul 20, 2017 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.