Advertisement
ndrwld

Wordpress Custom Taxonomy rewrite rules

Oct 5th, 2011
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. //PUT THIS CODE INTO YOUR functions.php
  2.  
  3. //Creating custom taxonomy:
  4.  
  5. add_action( 'init', 'create__taxonomy' );
  6.  
  7. function create__taxonomy() {
  8.  
  9. /* ##########################################################
  10.  * ### 'GALLERY' custom post type related custom taxonomy ###
  11.  * ##########################################################
  12.  */
  13.  
  14.     /*
  15.      * Hi, we are 'Gallery Categories' - custom taxonomy of 'gallery' custom post type.
  16.      */
  17.     $labels = array(
  18.         'name' => _x( 'Gallery - Categories', 'taxonomy general name', 'translation-domain' ),
  19.         'singular_name' => _x( 'Gallery Category', 'taxonomy singular name', 'translation-domain' ),
  20.         'search_items' =>  __( 'Search Categories', 'translation-domain' ),
  21.         'all_items' => __( 'All Categories - Gallery', 'translation-domain' ),
  22.         'parent_item' => __( 'Parent Category', 'translation-domain' ),
  23.         'parent_item_colon' => __( 'Parent Category:', 'translation-domain' ),
  24.         'edit_item' => __( 'Edit Category - Gallery', 'translation-domain' ),
  25.         'menu_name' => __( 'Gallery Categories', 'translation-domain' ),
  26.         'update_item' => __( 'Update Category - Gallery', 'translation-domain' ),
  27.         'add_new_item' => __( 'Add New Gallery Category', 'translation-domain' ),
  28.         'new_item_name' => __( 'New Gallery Category Name', 'translation-domain' ),
  29.       );    
  30.  
  31.     register_taxonomy('gallery_category','gallery',array(
  32.         'hierarchical' => true,
  33.         'labels' => $labels,
  34.         //'query_var' => true,
  35.         'query_var' => 'gallery_category',
  36.         'rewrite' => false
  37.                   ));
  38.                  
  39. // Register custom rewrite rules
  40. // @source: http://wpquestions.com/question/show/id/2841 (last comments)
  41.     global $wp_rewrite;
  42.  
  43.     $wp_rewrite->add_rewrite_tag('%gallery%', '([^/]+)', 'gallery=');
  44.     $wp_rewrite->add_rewrite_tag('%gallery_category%', '([^/]+)', 'gallery_category=');
  45.  
  46.     $wp_rewrite->add_permastruct('gallery_category', '/galeria/kategoria-%gallery_category%', false, EP_CATEGORIES);
  47.     $wp_rewrite->add_permastruct('gallery', '/galeria/kategoria-%gallery_category%/%gallery%', false);
  48.  
  49. }
  50.  
  51.  
  52. //Adding Rewrite rules:
  53. // @source: http://wpquestions.com/question/show/id/2841 (last comments)
  54.  
  55. add_filter('post_type_link', 'wp_gallery_permalinks', 10, 3);
  56. add_filter('term_link', 'wp_gallery_term_link', 10, 3);
  57.  
  58. function wp_gallery_permalinks($permalink, $post_id, $leavename) {
  59.  
  60.     $no_terms = 'no-category';
  61.     $post = get_post($post_id);
  62.  
  63.     if($post->post_type != 'gallery' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
  64.         return $permalink;
  65.  
  66.     $terms = wp_get_object_terms((int)$post->ID, 'gallery_category');
  67.  
  68.     if(is_wp_error($terms) || empty($terms)) {
  69.  
  70.         $term_slug = $no_terms;
  71.  
  72.     } else {
  73.         $term = reset($terms);
  74.  
  75.         if(is_object($term))
  76.             $term_slug = $term->slug;
  77.         else
  78.             $term_slug = $no_terms;
  79.     }
  80.    
  81.     $permalink = str_replace('%gallery_category%', $term_slug, $permalink);
  82.  
  83.     return $permalink;
  84. }
  85.  
  86.  
  87.  
  88. function wp_gallery_term_link($termlink, $term, $taxonomy) {
  89.     $termlink = str_replace('%gallery_category%', $term->slug, $termlink);
  90.     return $termlink;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement