Assigning CPT-onomy Terms  CPT-onomies Documentation  

Custom CPT-onomy Archive Pages

Description

As of version 1.2, CPT-onomies has implemented a simple, built-in method of setting up custom CPT-onomy archive pages that’s as easy as adding a rewrite rule with a few parameters. I’ve included a few samples that should help you get your feet wet.

Just a few notes before you get started:

  • The ‘cpt_onomy_archive=1′ parameter is required to make all of this work.
  • Be sure to flush your rewrite rules each time you edit them. Flush your rewrite rules by visiting Settings -> Permalinks and clicking “Save Changes”.
  • If you have multiple rewrite rules with the same base, like the first two examples below, the rule with the longer structure needs to go first.

Examples

Add this code to your functions.php file:

<?php
add_action( 'init', 'my_website_add_rewrite_rule' );
function my_website_add_rewrite_rule() {

   // Says that if the URL matches this rule, i.e. http://mywebsite.com/movies/steven-spielberg/tom-hanks/,
   // then it should display the 'movies' post type that are tagged with the first term (which should be
   // from the 'directors' CPT-onomy) and the second term (which should be from the 'actors' CPT-onomy).
   add_rewrite_rule( '^movies/([^/]*)/([^/]*)/?', 'index.php?post_type=movies&directors=$matches[1]&actors=$matches[2]&cpt_onomy_archive=1', 'top' );

   // Says that if the URL matches this rule, i.e. http://mywebsite.com/movies/steven-spielberg/,
   // then it should display the \'movies\' post type that are tagged with the first term (which should
   // be from the 'directors' CPT-onomy).
   add_rewrite_rule( '^movies/([^/]*)/?', 'index.php?post_type=movies&directors=$matches[1]&cpt_onomy_archive=1', 'top' );

   // Says that if the URL matches this rule, i.e. http://mywebsite.com/directors/steven-spielberg/,
   // then it should display all post types that are tagged with the first term (which should be from the 'directors' CPT-onomy).
   add_rewrite_rule( '^directors/([^/]*)/?', 'index.php?directors=$matches[1]&cpt_onomy_archive=1', 'top' );

}
?>
Assigning CPT-onomy Terms  CPT-onomies Documentation  
  • Jpl

    if i want to rewrite url for archive, can i use this in my function.php
    How i can add_rewrite_rule only if i want http://mywebsite.com/directors/

  • Junior Godoi

    AWESOME!!! Thanks Rachel, this plugins gets even better now =)

    • rachelcarden

      You’re welcome! Glad you like it!