Community Forums › Forums › Archived Forums › General Discussion › Custom Taxonomy Rewrite
- This topic has 4 replies, 2 voices, and was last updated 7 years ago by JPJP9.
-
AuthorPosts
-
April 5, 2017 at 8:03 pm #204363JPJP9Member
Rewrites drive me crazy!!!
My permalinks are right but I'm getting a 404 on the single page
Archive is http://www.website.com/products/kubota/
Single is Archive is http://www.website.com/products/kubota/zero-turn-motorsCPT:
function register_cpt_New() { $labels = array( 'name' => 'New Equipment', 'singular_name' => 'New Equipment', 'menu_name' => 'New Equipment', 'name_admin_bar' => 'New Equipment', 'add_new' => 'Add New', 'add_new_item' => 'Add New New Equipment', 'new_item' => 'New New Equipment', 'edit_item' => 'Edit New Equipment', 'view_item' => 'View New Equipment', 'all_items' => 'All New Equipment ', 'search_items' => 'Search New Equipment', 'parent_item_colon' => 'Parent New Equipment', 'not_found' => 'No New Equipment found.', 'not_found_in_trash' => 'No New Equipment found in Trash.', ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-admin-generic', 'query_var' => true, 'capability_type' => 'post', 'has_archive' => true, 'rewrite' => array( 'slug' => 'products/%product_tax%', 'with_front' => false ), 'has_archive' => 'product', 'hierarchical' => false, 'menu_position' => 2, 'supports' => array( 'title', 'editor', 'thumbnail' ,'genesis-cpt-archives-settings'), 'taxonomies' => array('product') ); register_post_type( 'new', $args ); }
Taxonomy:
function register_tax_Product() { $labels = array( 'name' => 'Product', 'singular_name' => 'Product', 'search_items' => 'Search Products', 'popular_items' => 'Popular Products', 'all_items' => 'All Products', 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => 'Edit Product', 'update_item' => 'Update Product', 'add_new_item' => 'Add New Product', 'new_item_name' => 'New Product Name', 'separate_items_with_commas' => 'Separate Products with commas', 'add_or_remove_items' => 'Add or remove Products', 'choose_from_most_used' => 'Choose from the most used Products', 'not_found' => 'No Products found.', 'menu_name' => 'Products', ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => array('slug' => 'products', 'with_front' => false), 'update_count_callback' => '_update_post_term_count', 'query_var' => true, ); register_taxonomy( 'product', array( 'new'), $args ); }
Filter:
function wt_product_permalinks( $post_link, $post ){ if ( is_object( $post ) && $post->post_type == 'new' ){ $terms = wp_get_object_terms( $post->ID, 'product' ); if( $terms ){ return str_replace( '%product_tax%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'wt_product_permalinks', 1, 2 );
April 6, 2017 at 6:58 am #204381Victor FontModeratorDid you resave permalinks after creating this code? See this note:
Note: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?April 6, 2017 at 6:46 pm #204413JPJP9MemberHi Victor,
Yes I did. I have this function in the posttypes.php file:
function wt_taxonomy_flush_rewrite() { global $wp_rewrite; $wp_rewrite->flush_rules(); } add_action('init', 'wt_taxonomy_flush_rewrite');
And I did it manually also.
April 10, 2017 at 8:43 pm #204601JPJP9Member? Anyone
April 17, 2017 at 9:37 pm #204912JPJP9MemberI'm still struggling with this rewrite.
It has to do with the str_replace as if I replace this line for the CPT
'rewrite' => array( 'slug' => 'products/%product_tax%', 'with_front' => false ),
with
'rewrite' => array( 'slug' => 'products/tax', 'with_front' => false ),
It works fine except using the word tax instead of the actual taxonomy. URLs are correct in all cases except that single throws a 404
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.