CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Alternate Custom CPT-onomy Archive Pages and Templates (5 posts)

  1. mpmchugh
    Posted 1 year ago #

    Two questions on archive pages and templates as described here:

    1. Is it possible to have an alternative structure for these? For instance, using the examples on the documentation page, if I wanted to have a section of a Movies site for directors, with a page listing their movies, their tv shows, etc., is there a way to have the urls for that section be in the form of /directors/director_name/movies

    2. Related to this, is there a way then to have an alternate archive template depending on the url structure used? For instance, in the "directors" section example above, I'd want the template to have sub-navigation focused on the director, whereas for a main /movies page I might want something different.

    Are either of these doable?



  2. Rachel Carden
    Plugin Author

    Posted 1 year ago #

    These are both do-able.

    1. Yes, just change up the rule:

    add_rewrite_rule( 'directors/([^/]+)/movies/?$', 'index.php?post_type=movies&directors=$matches[1]&cpt_onomy_archive=1', 'top' );

    2. You can't have an archive template dependent on the URL out of the box BUT you can have the archive-movies.php template and then, in the code, use the is_tax( 'directors' ) logic to only include your navigation on those pages OR you could make your own template file and use the 'template_include' filter to tell WordPress to use the specific file. Something like this:

    add_filter( 'template_include', 'my_website_template_include' );
    function my_website_template_include( $template ) {
       global $post_type;
       if ( is_archive() && $post_type == 'movies' && is_tax( 'directors' ) ) {
          if ( $custom_template = locate_template( 'archive-director-movies.php' ) )
             return $custom_template;
       return $template;

    I hope this helps!

  3. mpmchugh
    Posted 1 year ago #

    Thanks Rachel. That was very helpful. The first part for the archives URLs worked great.

    Is is possible to do the same for the single?
    i.e. can a single movie also be at a similar url, so something like:


    I guess part of the question is — is possible for a page to have two different URLs, or no?

    For the second part, what I actually need is a way for an alternative sub-navigation for the single template depending on it's context.

    So, when a movie was accessed directly, it would show in the URL form of:


    ...and then show the general movie sub-nav, but when accessed via the director's section (as above) it would have the director specific sub-nav.

    I'm guessing, if the page can have two URLs, as I described above, I can just do a conditional based on the URI and switch out the template part, but if not, I suspect I'd have to pass the page a variable in the URL.

    If it's not clear, what I'm trying to do is change the navigation just based on the context of the page on the site — how you get to it, not actually based on any differences in the content itself.

  4. Rachel Carden
    Plugin Author

    Posted 1 year ago #

    Check out the "How To Add Custom Templates Based On Custom Archive Pages" section on my Custom CPT-onomy Archive Pages page and see if that points you in the right direction.

    Basically, you create your rewrite rule and then you can tell WordPress to load a specific template based on conditionals.

  5. mpmchugh
    Posted 1 year ago #

    Thanks! I'll have a look, and see if that helps.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic


No tags yet.