luisabarca

CPT Custom permalinks

Oct 21st, 2011
433
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_filter('post_type_link', 'products_type_link', 1, 3);
  2.  
  3. function products_type_link($url, $post = null, $leavename = false)
  4. {
  5.     // products only
  6.         if ($post->post_type != self::CUSTOM_TYPE_NAME) {
  7.             return $url;
  8.         }
  9.        
  10.         $post_id = $post->ID;
  11.    
  12.         $taxonomy = 'product_type';
  13.         $taxonomy_tag = '%' . $taxonomy . '%';
  14.    
  15.         // Check if exists the product type tag
  16.         if (strpos($taxonomy_tag, $url) < 0) {
  17.             // replace taxonomy tag with the term slug: /products/%product_type%/samsumng/productname
  18.             $url = str_replace($taxonomy_tag, '', $url);
  19.         } else {
  20.             // Get the terms
  21.             $terms = wp_get_post_terms($post_id, $taxonomy);
  22.    
  23.             if (is_array($terms) && sizeof($terms) > 0) {
  24.                 $category = $terms[0];
  25.                 // replace taxonomy tag with the term slug: /products/%product_type%/samsumng/productname
  26.                 $url = str_replace($taxonomy_tag, $category->slug, $url);
  27.             }
  28.         }
  29.    
  30.         /*
  31.          * Brand tags
  32.          */
  33.         $brand = 'product_brand';
  34.         $brand_tag = '%' . $brand . '%';
  35.    
  36.         // Check if exists the brand tag
  37.         if (strpos($brand_tag, $url) < 0) {
  38.             return str_replace($brand_tag, '', $url);
  39.         }
  40.        
  41.         $brand_terms = wp_get_post_terms($post_id, $brand);
  42.    
  43.         if (is_array($brand_terms) && sizeof($brand_terms) > 0) {
  44.             $brand_category = $brand_terms[0];
  45.         }
  46.    
  47.         // replace brand tag with the term slug: /products/cell-phone/%product_brand%/productname
  48.         return str_replace($brand_tag, $brand_category->slug, $url);
  49. }
  50.  
  51. function products_add_rewrite_rules()
  52. {
  53.     global $wp_rewrite;
  54.     global $wp_query;
  55.  
  56.     register_post_type('products', array(
  57.         'label' => 'Products',
  58.         'description' => 'GVS products and services.',
  59.         'public' => true,
  60.         'show_ui' => true,
  61.         'show_in_menu' => true,
  62.         'capability_type' => 'post',
  63.         'hierarchical' => true,
  64.         'rewrite' => array('slug' => 'products'),
  65.         'query_var' => true,
  66.         'has_archive' => true,
  67.         'menu_position' => 6,
  68.         'supports' => array(
  69.             'title',
  70.             'editor',
  71.             'excerpt',
  72.             'trackbacks',
  73.             'revisions',
  74.             'thumbnail',
  75.             'author'),
  76.         'labels' => array (
  77.             'name' => 'Products',
  78.             'singular_name' => 'product',
  79.             'menu_name' => 'Products',
  80.             'add_new' => 'Add product',
  81.             'add_new_item' => 'Add New product',
  82.             'edit' => 'Edit',
  83.             'edit_item' => 'Edit product',
  84.             'new_item' => 'New product',
  85.             'view' => 'View product',
  86.             'view_item' => 'View product',
  87.             'search_items' => 'Search Products',
  88.             'not_found' => 'No Products Found',
  89.             'not_found_in_trash' => 'No Products Found in Trash',
  90.             'parent' => 'Parent product'),
  91.         )
  92.     );
  93.  
  94.     register_taxonomy('product-categories', 'products', array(
  95.         'hierarchical' => true,
  96.         'label' => 'Product Categories',
  97.         'show_ui' => true,
  98.         'query_var' => true,
  99.         'rewrite' => array('slug' => 'products'),
  100.         'singular_label' => 'Product Category')
  101.     );
  102.  
  103.     $wp_rewrite->extra_permastructs['products'][0] = "/products/%product_type%/%product_brand%/%products%";
  104.  
  105.         // product archive
  106.         add_rewrite_rule("products/?$", 'index.php?post_type=products', 'top');
  107.  
  108.         /*
  109.          * Product brands
  110.          */
  111.         add_rewrite_rule("products/([^/]+)/([^/]+)/?$", 'index.php?post_type=products&product_brand=$matches[2]', 'top');
  112.         add_rewrite_rule("products/([^/]+)/([^/]+)/page/([0-9]{1,})/?$", 'index.php?post_type=products&product_brand=$matches[2]&paged=$matches[3]', 'top');
  113.        
  114.         /*
  115.          * Product type archive
  116.          */
  117.         add_rewrite_rule("products/([^/]+)/?$", 'index.php?post_type=products&product_type=$matches[1]', 'top');    
  118.         add_rewrite_rule("products/([^/]+)/page/([0-9]{1,})/?$", 'index.php?post_type=products&product_type=$matches[1]&paged=$matches[1]', 'bottom'); // product type pagination
  119.    
  120.         // single product
  121.         add_rewrite_rule("products/([^/]+)/([^/]+)/([^/]+)/?$", 'index.php?post_type=products&product_type=$matches[1]&product_brand=$matches[2]&products=$matches[3]', 'top');
  122.    
  123.  
  124.  
  125.     flush_rewrite_rules();
  126. }
  127.  
  128. add_action('init', 'products_add_rewrite_rules');
RAW Paste Data