Advertisement
Twansparant

Permalink rewrites for post type + 2 (hierarchical) taxonomy

Jun 10th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.07 KB | None | 0 0
  1. /* ==========================================================================
  2.    Post Types
  3.    ========================================================================== */
  4.    
  5. add_action( 'init', 'my_custom_post_auto' );
  6. function my_custom_post_auto() {
  7.       $labels = array(
  8.         'name'               => _x( 'Auto', 'post type general name' ),
  9.         'singular_name'      => _x( 'auto', 'post type singular name' ),
  10.         'add_new'            => _x( 'Voeg toe', 'auto' ),
  11.         'add_new_item'       => __( 'Voeg nieuwe auto toe' ),
  12.         'edit_item'          => __( 'Edit auto' ),
  13.         'new_item'           => __( 'Nieuwe auto' ),
  14.         'all_items'          => __( 'Alle autos' ),
  15.         'view_item'          => __( 'Bekijk auto' ),
  16.         'search_items'       => __( 'Zoek auto' ),
  17.         'not_found'          => __( 'Geen autos gevonden' ),
  18.         'not_found_in_trash' => __( 'Geen autos gevonden' ),
  19.         'parent_item_colon'  => '',
  20.         'menu_name'          => 'Autos'
  21.       );
  22.       $args = array(
  23.         'labels'        => $labels,
  24.         'description'   => '',
  25.         'public'        => true,
  26.         'menu_position' => 5,
  27.         'supports'      => array( 'title', 'editor', 'thumbnail'),
  28.         'has_archive'   => 'autos',
  29.         'rewrite'       => array('slug' => 'merk/%auto-merk%/%auto-bouwjaar%' ),
  30.       );
  31.       register_post_type( 'auto', $args );
  32. }
  33.  
  34.  
  35. /* ==========================================================================
  36.    Taxonomies
  37.    ========================================================================== */
  38.  
  39. /* Auto Merk taxonomy */
  40. function auto_merk_taxonomy() {
  41.   $labels = array(
  42.     'name'              => __('Automerken'),
  43.     'singular_name'     => __('Automerk'),
  44.     'search_items'      => __('Zoek automerken'),
  45.     'all_items'         => __('Alle automerken'),
  46.     'parent_item'       => __('Hoofdautomerk'),
  47.     'parent_item_colon' => __('Hoofdautomerk:'),
  48.     'edit_item'         => __('Bewerk automerk'),
  49.     'update_item'       => __('Werk automerk bij'),
  50.     'add_new_item'      => __('Voeg nieuw automerk toe'),
  51.     'new_item_name'     => __('Nieuw automerk'),
  52.     'menu_name'         => __('Automerken')
  53.   );
  54.   $args = array(
  55.     'labels'            => $labels,
  56.     'hierarchical'      => true,
  57.     'query_var'         => true,
  58.     'rewrite'           => array('slug' => 'merk', 'hierarchical' => true)
  59.   );
  60.   register_taxonomy('auto-merk', 'auto', $args);
  61. }
  62. add_action( 'init', 'auto_merk_taxonomy' );
  63.  
  64.  
  65. /* Auto Bouwjaar taxonomy */
  66. function auto_bouwjaar_taxonomy() {
  67.   $labels = array(
  68.     'name'              => __('Bouwjaren'),
  69.     'singular_name'     => __('Bouwjaar'),
  70.     'search_items'      => __('Zoek bouwjaren'),
  71.     'all_items'         => __('Alle bouwjaren'),
  72.     'parent_item'       => __('Hoofdbouwjaar'),
  73.     'parent_item_colon' => __('Hoofdbouwjaar:'),
  74.     'edit_item'         => __('Bewerk bouwjaar'),
  75.     'update_item'       => __('Werk bouwjaar bij'),
  76.     'add_new_item'      => __('Voeg nieuw bouwjaar toe'),
  77.     'new_item_name'     => __('Nieuw bouwjaar'),
  78.     'menu_name'         => __('Bouwjaren')
  79.   );
  80.   $args = array(
  81.     'labels'            => $labels,
  82.     'hierarchical'      => true,
  83.     'query_var'         => true,
  84.     'rewrite'           => array('slug' => 'merk', 'hierarchical' => true)
  85.   );
  86.   register_taxonomy('auto-bouwjaar', 'auto', $args);
  87. }
  88. add_action( 'init', 'auto_bouwjaar_taxonomy' );
  89.  
  90.  
  91.  
  92. /* ==========================================================================
  93.    Permalink Structure
  94.    ========================================================================== */
  95.    
  96. function filter_post_type_link($link, $post) {
  97.     if ($post->post_type != 'auto')
  98.       return $link;
  99.    
  100.     if ($cats = get_the_terms($post->ID, 'auto-merk')) { // return both parent and child terms alphabetically
  101.         $link = str_replace('%auto-merk%', get_taxonomy_parents(array_pop($cats)->term_id, 'auto-merk', false, '/', true), $link);
  102.         //$family_id = print_taxonomy_ranks( get_the_terms($post->ID, 'auto-merk') ); // get only child term id
  103.         //$link = str_replace('%auto-merk%', get_taxonomy_parents($family_id, 'auto-merk', false, '/', true), $link);
  104.     }
  105.     return $link;
  106. }
  107. add_filter('post_type_link', 'filter_post_type_link', 10, 2);
  108.  
  109.  
  110. /*
  111.  * rank hierarchical taxonomy terms:
  112.  * http://wordpress.stackexchange.com/questions/37285/custom-taxonomy-get-the-terms-listing-in-order-of-parent-child
  113.  */
  114. function print_taxonomy_ranks($terms = '' ){
  115.     // check input
  116.     if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) )
  117.         return;
  118.     // set id variables to 0 for easy check
  119.     $order_id = $family_id = $subfamily_id = 0;
  120.    
  121.     // get order
  122.     foreach ( $terms as $term ) {
  123.         if ( $order_id || $term->parent )
  124.             continue;
  125.         $order_id  = $term->term_id;
  126.         $order     = $term->name;
  127.     }
  128.     // get family
  129.     foreach ( $terms as $term ) {
  130.         if ( $family_id || $order_id != $term->parent )
  131.             continue;
  132.         $family_id = $term->term_id;
  133.         $family    = $term->name;
  134.     }
  135.     // get subfamily
  136.     foreach ( $terms as $term ) {
  137.         if ( $subfamily_id || $family_id != $term->parent )
  138.             continue;
  139.         $subfamily_id = $term->term_id;
  140.         $subfamily    = $term->name;
  141.     }
  142.     // output
  143.     return $family_id;
  144. }
  145.  
  146. /*
  147.  * Get taxonomy parents:
  148.  * http://wordpress.stackexchange.com/questions/101316/custom-post-type-with-two-hierarchical-custom-taxonomies-strategy-to-generate-b
  149.  */
  150. function get_taxonomy_parents($id, $taxonomy, $link = false, $nicename = false, $visited = array()) {    
  151.     // removed $seperator="/" after $link, otherwise there was a double slash before the post name in the link    
  152.     $chain = '';  
  153.     $parent = &get_term($id, $taxonomy);
  154.  
  155.     if (is_wp_error($parent)) {
  156.         return $parent;
  157.     }
  158.  
  159.     if ($nicename)    
  160.         $name = $parent -> slug;        
  161.     else    
  162.         $name = $parent -> name;
  163.        
  164.     if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, array($visited)) ) {    
  165.         $visited[] = $parent -> parent;    
  166.         $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited); //removed $seperator after $link
  167.         $chain .= "/"; //add a / after every category
  168.     }
  169.  
  170.     if ($link) {
  171.         // nothing, can't get this working :(
  172.     } else    
  173.         $chain .= $name; //don't need the . $separator anymore
  174.     return $chain;
  175. }
  176.  
  177. function filter_post_type_link_due($link, $post) {
  178.     if ($post->post_type != 'auto')
  179.         return $link;
  180.  
  181.     if ($cats = get_the_terms($post->ID, 'auto-bouwjaar')) {
  182.         $link = str_replace('%auto-bouwjaar%', get_taxonomy_parents(array_pop($cats)->term_id, 'auto-bouwjaar', false, '/', true), $link);
  183.     }
  184.     return $link;
  185. }
  186. add_filter('post_type_link', 'filter_post_type_link_due', 10, 2);
  187.  
  188.  
  189.  
  190. /* ==========================================================================
  191.    Rewrite Rules
  192.    ========================================================================== */
  193.  
  194. add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
  195. function mmp_rewrite_rules($rules) {
  196.  
  197.     //TIPOLOGIA
  198.     $tipologie = get_terms('auto-merk', array(
  199.         'hide_empty' => false
  200.     ));
  201.  
  202.     foreach ($tipologie as $tipologia) {
  203.         $slugs_tipologie [] = $tipologia->slug;
  204.     }
  205.  
  206.     $num_tipologie = count($slugs_tipologie);
  207.     $regex_tipologia = '';
  208.     if( $num_tipologie > 0) {
  209.         for( $i=0; $i < $num_tipologie-1; $i++ ) {
  210.             $regex_tipologia .= $slugs_tipologie[$i] .'|';
  211.         }
  212.         $regex_tipologia .= array_pop($slugs_tipologie);
  213.     }
  214.  
  215.     $ee1 = merk."/($regex_tipologia)/(.+)/(.+)/(.+)/(.+)/?$";
  216.     $ee2 = merk."/($regex_tipologia)/?$";
  217.     $ee3 = merk."/($regex_tipologia)/(.+)/?$";
  218.     $ee4 = merk."/(.+)/?$";
  219.  
  220.     $newRules  = array();
  221.     $newRules[$ee1] = 'index.php?auto=$matches[5]';
  222.     $newRules[$ee2] = 'index.php?auto-merk=$matches[1]';
  223.     $newRules[$ee3] = 'index.php?auto-merk=$matches[1]&auto-bouwjaar=$matches[2]';
  224.     $newRules[$ee4] = 'index.php?auto-bouwjaar=$matches[1]';
  225.     return array_merge($newRules, $rules);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement