CPT Permalink (parent/child)
-
I have a Custom Post Type that I am trying to display the parent and child categories in the permalink but with a rewrite function it’s only displaying what is typed in the ‘slug’ function.
Is there a way to display the parent and child categories in the permalink?
e.g. https://yoursite.com/ca-en/*parent_category*/*child_category*/*page_name*-
This topic was modified 3 years, 4 months ago by
nate.ads.
-
This topic was modified 3 years, 4 months ago by
-
You can use the
register_taxonomyfunction to register a custom taxonomy for your post type, and then use the rewrite parameter to set the desired permalink structure. Here’s an example:register_taxonomy( 'custom_taxonomy', 'custom_post_type', array( 'hierarchical' => true, 'label' => 'Custom Taxonomy', 'query_var' => true, 'rewrite' => array( 'slug' => 'ca-en/%custom_taxonomy_parent%/%custom_taxonomy%', 'with_front' => false ) ) );In the above example,
%custom_taxonomy_parent%and%custom_taxonomy%are placeholders that will be replaced with the actual parent and child categories when the permalink is generated. The with_front parameter is set to false to remove the front base of the URL.Additionally, you need to add filter post_type_link to modify the generated link.
function custom_post_type_link( $post_link, $id = 0, $leavename ) { $post = get_post($id); if ( is_object( $post ) && $post->post_type == 'custom_post_type' ){ $terms = wp_get_object_terms( $post->ID, 'custom_taxonomy' ); if( $terms ){ return str_replace( '%custom_taxonomy%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'custom_post_type_link', 1, 3 );This is just an example, you may need to adjust the code to match your specific requirements.
Thank you for your reply @faisalahammad, unfortunately I am doing something wrong but I cannot figure out where.
Below is all my code for the CPTfunction brochure_post() { $labels = array( 'name' => _x( 'Brochure Product', 'post type general name' ), 'singular_name' => _x( 'Brochure Product', 'post type singular name' ), 'add_new' => _x( 'Add New', 'product' ), 'add_new_item' => __( 'Add New Brochure' ), 'edit_item' => __( 'Edit Brochure' ), 'new_item' => __( 'New Brochure' ), 'all_items' => __( 'All Brochures' ), 'view_item' => __( 'View Brochure' ), 'search_items' => __( 'Search Brochures' ), 'not_found' => __( 'No Brochure found' ), 'not_found_in_trash' => __( 'No Brochure found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Brochures', ); $args = array( 'labels' => $labels, 'description' => 'Holds Brochure and Brochure specific data', 'public' => true, 'show_ui' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'category', 'tags', 'page-attributes' ), 'has_archive' => false, 'taxonomies' => array( 'category' ), ); register_post_type( 'brochures', $args ); } add_action( 'init', 'brochure_post', 0 ); register_taxonomy( 'custom_taxonomy', 'custom_post_type', array( 'hierarchical' => true, 'label' => 'Custom Taxonomy', 'query_var' => true, 'rewrite' => array( 'slug' => '/%custom_taxonomy_parent%/%custom_taxonomy%', 'with_front' => false, ) ) ); function custom_post_type_link( $post_link, $id = 0, $leavename ) { $post = get_post($id); if ( is_object( $post ) && $post->post_type == 'custom_post_type' ) { $terms = wp_get_object_terms( $post->ID, 'custom_taxonomy' ); if( $terms ) { return str_replace( '%custom_taxonomy%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'custom_post_type_link', 1, 3 );Problem solved!
I used the below code to create a CPT and it has the ability to rewrite a part of the URL in the browser address bar, and depending on the categories used in the Dashboard will display in the address bar.function brochure_post() { $labels = array( 'name' => _x( 'Brochure Product', 'post type general name' ), 'singular_name' => _x( 'Brochure Product', 'post type singular name' ), 'add_new' => _x( 'Add New', 'product' ), 'add_new_item' => __( 'Add New Brochure' ), 'edit_item' => __( 'Edit Brochure' ), 'new_item' => __( 'New Brochure' ), 'all_items' => __( 'All Brochures' ), 'view_item' => __( 'View Brochure' ), 'search_items' => __( 'Search Brochures' ), 'not_found' => __( 'No Brochure found' ), 'not_found_in_trash' => __( 'No Brochure found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Brochures', ); $args = array( 'labels' => $labels, 'description' => 'Holds Brochure and Brochure specific data', 'public' => true, 'show_ui' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'category', 'tags', 'page-attributes' ), 'has_archive' => false, 'taxonomies' => array( 'category' ), 'rewrite' => array( 'slug' => 'products/%category%', 'with_front' => false ), ); register_post_type( 'brochures', $args ); } add_action( 'init', 'brochure_post', 0 ); function brochure_permalink( $post_link, $id = 0 ){ $post = get_post($id); $terms = wp_get_object_terms( $post->ID, 'category' ); if( $terms ){ return str_replace( '%category%' , $terms[0]->slug , $post_link ); } else { return str_replace( '%category%/' , '' , $post_link ); } return $post_link; } add_filter( 'post_type_link', 'brochure_permalink', 1, 3 );-
This reply was modified 3 years, 4 months ago by
nate.ads.
-
This reply was modified 3 years, 4 months ago by
The topic ‘CPT Permalink (parent/child)’ is closed to new replies.