Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('post_type_link', 'products_type_link', 1, 3);
- function products_type_link($url, $post = null, $leavename = false)
- {
- // products only
- if ($post->post_type != self::CUSTOM_TYPE_NAME) {
- return $url;
- }
- $post_id = $post->ID;
- $taxonomy = 'product_type';
- $taxonomy_tag = '%' . $taxonomy . '%';
- // Check if exists the product type tag
- if (strpos($taxonomy_tag, $url) < 0) {
- // replace taxonomy tag with the term slug: /products/%product_type%/samsumng/productname
- $url = str_replace($taxonomy_tag, '', $url);
- } else {
- // Get the terms
- $terms = wp_get_post_terms($post_id, $taxonomy);
- if (is_array($terms) && sizeof($terms) > 0) {
- $category = $terms[0];
- // replace taxonomy tag with the term slug: /products/%product_type%/samsumng/productname
- $url = str_replace($taxonomy_tag, $category->slug, $url);
- }
- }
- /*
- * Brand tags
- */
- $brand = 'product_brand';
- $brand_tag = '%' . $brand . '%';
- // Check if exists the brand tag
- if (strpos($brand_tag, $url) < 0) {
- return str_replace($brand_tag, '', $url);
- }
- $brand_terms = wp_get_post_terms($post_id, $brand);
- if (is_array($brand_terms) && sizeof($brand_terms) > 0) {
- $brand_category = $brand_terms[0];
- }
- // replace brand tag with the term slug: /products/cell-phone/%product_brand%/productname
- return str_replace($brand_tag, $brand_category->slug, $url);
- }
- function products_add_rewrite_rules()
- {
- global $wp_rewrite;
- global $wp_query;
- register_post_type('products', array(
- 'label' => 'Products',
- 'description' => 'GVS products and services.',
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'capability_type' => 'post',
- 'hierarchical' => true,
- 'rewrite' => array('slug' => 'products'),
- 'query_var' => true,
- 'has_archive' => true,
- 'menu_position' => 6,
- 'supports' => array(
- 'title',
- 'editor',
- 'excerpt',
- 'trackbacks',
- 'revisions',
- 'thumbnail',
- 'author'),
- 'labels' => array (
- 'name' => 'Products',
- 'singular_name' => 'product',
- 'menu_name' => 'Products',
- 'add_new' => 'Add product',
- 'add_new_item' => 'Add New product',
- 'edit' => 'Edit',
- 'edit_item' => 'Edit product',
- 'new_item' => 'New product',
- 'view' => 'View product',
- 'view_item' => 'View product',
- 'search_items' => 'Search Products',
- 'not_found' => 'No Products Found',
- 'not_found_in_trash' => 'No Products Found in Trash',
- 'parent' => 'Parent product'),
- )
- );
- register_taxonomy('product-categories', 'products', array(
- 'hierarchical' => true,
- 'label' => 'Product Categories',
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => 'products'),
- 'singular_label' => 'Product Category')
- );
- $wp_rewrite->extra_permastructs['products'][0] = "/products/%product_type%/%product_brand%/%products%";
- // product archive
- add_rewrite_rule("products/?$", 'index.php?post_type=products', 'top');
- /*
- * Product brands
- */
- add_rewrite_rule("products/([^/]+)/([^/]+)/?$", 'index.php?post_type=products&product_brand=$matches[2]', 'top');
- add_rewrite_rule("products/([^/]+)/([^/]+)/page/([0-9]{1,})/?$", 'index.php?post_type=products&product_brand=$matches[2]&paged=$matches[3]', 'top');
- /*
- * Product type archive
- */
- add_rewrite_rule("products/([^/]+)/?$", 'index.php?post_type=products&product_type=$matches[1]', 'top');
- add_rewrite_rule("products/([^/]+)/page/([0-9]{1,})/?$", 'index.php?post_type=products&product_type=$matches[1]&paged=$matches[1]', 'bottom'); // product type pagination
- // single product
- add_rewrite_rule("products/([^/]+)/([^/]+)/([^/]+)/?$", 'index.php?post_type=products&product_type=$matches[1]&product_brand=$matches[2]&products=$matches[3]', 'top');
- flush_rewrite_rules();
- }
- add_action('init', 'products_add_rewrite_rules');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement