WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. mpmchugh
    Member
    Posted 4 months ago #

    Two questions on archive pages and templates as described here:
    http://wpdreamer.com/plugins/cpt-onomies/documentation/custom-archive-pages/...

    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?

    Thanks,
    Michael

    https://wordpress.org/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 4 months 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
    Member
    Posted 4 months 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:

    /directors/martin-scorcese/movies/mean-streets

    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:

    /movies/mean-streets

    ...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
    Member
    Plugin Author

    Posted 4 months 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
    Member
    Posted 4 months ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.