Archived topic
Custom Taxonomy / Slug
6 replies · Started by Jan on November 3, 2022
Hi David,
I'm using a CPT together with a custom taxonomy (wg_chat_category). The slug currently pointing to the different categories needs to change from /wg_chat_category/ to /hilfe-chat/
old: domain.de/wg_chat_category/general/
new: domain.de/hilfe-chat/general/
This is the PHP that I'm currently using:
if ( ! function_exists( 'wg_chat_category' ) ) {
// Register Custom Taxonomy
function wg_chat_category() {
$labels = array(
'name' => _x( 'Chat Categories', 'Taxonomy General Name', 'wg_chat_category' ),
'singular_name' => _x( 'Chat Category', 'Taxonomy Singular Name', 'wg_chat_category' ),
'menu_name' => __( 'Chat Category', 'wg_chat_category' ),
'all_items' => __( 'All Chat Categories', 'wg_chat_category' ),
'parent_item' => __( 'Parent Chat Category', 'wg_chat_category' ),
'parent_item_colon' => __( 'Parent Chat Category:', 'wg_chat_category' ),
'new_item_name' => __( 'New Chat Category Name', 'wg_chat_category' ),
'add_new_item' => __( 'Add New Chat Category', 'wg_chat_category' ),
'edit_item' => __( 'Edit Chat Category', 'wg_chat_category' ),
'update_item' => __( 'Update Chat Category', 'wg_chat_category' ),
'view_item' => __( 'View Chat Category', 'wg_chat_category' ),
'separate_items_with_commas' => __( 'Separate Chat Categories with commas', 'wg_chat_category' ),
'add_or_remove_items' => __( 'Add or remove Chat Categories', 'wg_chat_category' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wg_chat_category' ),
'popular_items' => __( 'Popular Chat Categories', 'wg_chat_category' ),
'search_items' => __( 'Search Chat Category', 'wg_chat_category' ),
'not_found' => __( 'Not Found', 'wg_chat_category' ),
'no_terms' => __( 'No Chat Categories', 'wg_chat_category' ),
'items_list' => __( 'Chat Category list', 'wg_chat_category' ),
'items_list_navigation' => __( 'Chat Category list navigation', 'wg_chat_category' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_tagcloud' => false,
);
register_taxonomy( 'wg_chat_category', array( 'wg_seo_chat' ), $args );
}
add_action( 'init', 'wg_chat_category', 0 );
}
How would you modify the PHP to change the slug?
Any advice is much appreciated.
Thanks,
Jan
Hi Jan,
What permalink structure do you have in Settings > Permalinks?
Hi Fernando,
the link structure is set to: /%category%/%postname%/
Best,
Jan
Hi Jan,
Any chance you can check with WP's support team first as this isn't a theme-related question?
Please refer to this guideline:
https://generatepress.com/what-support-includes/
Alternatively, you can try posting the question in a WP general forum like this one:
https://wordpress.stackexchange.com/
Thanks for your understanding :)
Hi Leo,
meanwhile I was able to dig out this valuable resource that provides a solution to the problem. I'm sharing this as others may find this useful also.
The rewrite of the slugs is now working well ;-)
What I'm struggling with is the slug of the CPT Archive page. The standard slug is hilfe-chat. When I use...
$rewrite = array(
'slug' => 'hilfe-chat',
...the CPT Archive page come up, but the Category Archives don't (404).
For testing purpose I added an "s" to the slug in the PHP...
$rewrite = array(
'slug' => 'hilfe-chats',
As a result the Category Archives work well but the CPT Archive throws throws an 404 error.
Any thoughts on how to resolve this conflict?
Thanks, Jan
--- Full copy of the PHP ---
if ( ! function_exists('wg_seo_chat') ) {
// Register Custom Post Type
function wg_seo_chat() {
$labels = array(
'name' => _x( 'Chats', 'Post Type General Name', 'wg_seo_chat' ),
'singular_name' => _x( 'Chat', 'Post Type Singular Name', 'wg_seo_chat' ),
'menu_name' => __( 'Chat', 'wg_seo_chat' ),
'name_admin_bar' => __( 'Chat', 'wg_seo_chat' ),
'archives' => __( 'Chat Archives', 'wg_seo_chat' ),
'attributes' => __( 'Chat Attributes', 'wg_seo_chat' ),
'parent_item_colon' => __( 'Parent Chat:', 'wg_seo_chat' ),
'all_items' => __( 'All Chats', 'wg_seo_chat' ),
'add_new_item' => __( 'Add New Chat', 'wg_seo_chat' ),
'add_new' => __( 'Add New', 'wg_seo_chat' ),
'new_item' => __( 'New Chat', 'wg_seo_chat' ),
'edit_item' => __( 'Edit Chat', 'wg_seo_chat' ),
'update_item' => __( 'Update Chat', 'wg_seo_chat' ),
'view_item' => __( 'View Chat', 'wg_seo_chat' ),
'view_items' => __( 'View Chats', 'wg_seo_chat' ),
'search_items' => __( 'Search Chat', 'wg_seo_chat' ),
'not_found' => __( 'Not found', 'wg_seo_chat' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wg_seo_chat' ),
'featured_image' => __( 'Featured Image', 'wg_seo_chat' ),
'set_featured_image' => __( 'Set featured image', 'wg_seo_chat' ),
'remove_featured_image' => __( 'Remove featured image', 'wg_seo_chat' ),
'use_featured_image' => __( 'Use as featured image', 'wg_seo_chat' ),
'insert_into_item' => __( 'Insert into Chat', 'wg_seo_chat' ),
'uploaded_to_this_item' => __( 'Uploaded to this Chat', 'wg_seo_chat' ),
'items_list' => __( 'Chat list', 'wg_seo_chat' ),
'items_list_navigation' => __( 'Chat list navigation', 'wg_seo_chat' ),
'filter_items_list' => __( 'Filter Chat list', 'wg_seo_chat' ),
);
$rewrite = array(
'slug' => 'hilfe-chats',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'Chat', 'wg_seo_chat' ),
'taxonomies' => array('wg_chat_category'),
'description' => __( 'Shares answers for user questions about SEO', 'wg_seo_chat' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', 'excerpt' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-chat',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'wg_seo_chat', $args );
}
add_action( 'init', 'wg_seo_chat', 0 );
}
if ( ! function_exists( 'wg_chat_category' ) ) {
// Register Custom Taxonomy
function wg_chat_category() {
$labels = array(
'name' => _x( 'Chat Categories', 'Taxonomy General Name', 'wg_chat_category' ),
'singular_name' => _x( 'Chat Category', 'Taxonomy Singular Name', 'wg_chat_category' ),
'menu_name' => __( 'Chat Category', 'wg_chat_category' ),
'all_items' => __( 'All Chat Categories', 'wg_chat_category' ),
'parent_item' => __( 'Parent Chat Category', 'wg_chat_category' ),
'parent_item_colon' => __( 'Parent Chat Category:', 'wg_chat_category' ),
'new_item_name' => __( 'New Chat Category Name', 'wg_chat_category' ),
'add_new_item' => __( 'Add New Chat Category', 'wg_chat_category' ),
'edit_item' => __( 'Edit Chat Category', 'wg_chat_category' ),
'update_item' => __( 'Update Chat Category', 'wg_chat_category' ),
'view_item' => __( 'View Chat Category', 'wg_chat_category' ),
'separate_items_with_commas' => __( 'Separate Chat Categories with commas', 'wg_chat_category' ),
'add_or_remove_items' => __( 'Add or remove Chat Categories', 'wg_chat_category' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wg_chat_category' ),
'popular_items' => __( 'Popular Chat Categories', 'wg_chat_category' ),
'search_items' => __( 'Search Chat Category', 'wg_chat_category' ),
'not_found' => __( 'Not Found', 'wg_chat_category' ),
'no_terms' => __( 'No Chat Categories', 'wg_chat_category' ),
'items_list' => __( 'Chat Category list', 'wg_chat_category' ),
'items_list_navigation' => __( 'Chat Category list navigation', 'wg_chat_category' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'hilfe-chat')
);
register_taxonomy( 'wg_chat_category', array( 'wg_seo_chat' ), $args );
}
add_action( 'init', 'wg_chat_category', 0 );
}
/** Rewrite rule to handle third level slug */
function chat_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'wg_chat_category',
'hide_empty' => false,
) );
$post_type = 'wg_seo_chat';
foreach ($terms as $term) {
$rules['hilfe-chat/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&wg_seo_chat=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'chat_cpt_generating_rule');
/** Inject the chat category into the CPT Link */
function change_link( $permalink, $post ) {
if( $post->post_type == 'wg_seo_chat' ) {
$chat_terms = get_the_terms( $post, 'wg_chat_category' );
$term_slug = '';
if( ! empty( $chat_terms ) ) {
foreach ( $chat_terms as $term ) {
// The featured chat will have another category which is the main one
if( $term->slug == 'featured' ) {
continue;
}
$term_slug = $term->slug;
break;
}
}
$permalink = get_home_url() ."/hilfe-chat/" . $term_slug . '/' . $post->post_name;
}
return $permalink;
}
add_filter('post_type_link',"change_link",10,2);
Hi Jan,
I would also suggest posting it in WP's support forum as it's not theme related.
And if you are not familiar with code, I would recommend using a plugin eg. Custom Post Type UI to create your CPT.
Hi David,
…we are probably looking at another pine of beer at the next Octoberfest for you because my next iteration did not bring the desired solution ;-/
I did follow Yings advice and found this solution.
Their advise is to add…
'has_archive' => 'hilfe-chat',
…to the rewrite rule.
I did just that and flushed the rewrite rules, but the conflict is still there.
$rewrite = array(
'slug' => 'hilfe-chats',
'has_archive' => 'hilfe-chat',
Any thoughts from the expert is much appropriate by the brand new life-time GPP subscriber ;-)
Best,
Jan