Hi Everybody,
I was creating Custom Post Types without a plugin and it is working good.
It’s showing URL like this
sitename[.]com/cars/post-url/
My Problem is:
What I wanted to show like this
sitename[.]com/category-name/post-url/
something like this
sitename[.]com/honda/honda-price-in-2022/
CPT Code
add_action( 'init', 'your_car_register_post_type' );
function your_car_register_post_type() {
$args = [
'label' => esc_html__( 'Cars', 'text-domain' ),
'labels' => [
'menu_name' => esc_html__( 'Cars', 'your-car' ),
'name_admin_bar' => esc_html__( 'Car', 'your-car' ),
'add_new' => esc_html__( 'Add Car', 'your-car' ),
'add_new_item' => esc_html__( 'Add new Car', 'your-car' ),
'new_item' => esc_html__( 'New Car', 'your-car' ),
'edit_item' => esc_html__( 'Edit Car', 'your-car' ),
'view_item' => esc_html__( 'View Car', 'your-car' ),
'update_item' => esc_html__( 'View Car', 'your-car' ),
'all_items' => esc_html__( 'All Cars', 'your-car' ),
'search_items' => esc_html__( 'Search Cars', 'your-car' ),
'parent_item_colon' => esc_html__( 'Parent Car', 'your-car' ),
'not_found' => esc_html__( 'No Cars found', 'your-car' ),
'not_found_in_trash' => esc_html__( 'No Cars found in Trash', 'your-car' ),
'name' => esc_html__( 'Cars', 'your-car' ),
'singular_name' => esc_html__( 'Car', 'your-car' ),
],
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite_no_front' => false,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-car',
'supports' => [
'title',
'editor',
'author',
'thumbnail',
'custom-fields',
'comments',
],
'taxonomies' => [
'category',
],
'rewrite' => true,
];
register_post_type( 'car', $args );
}
I was able to remove cars (CPT) from the URL using following code.
function abc_remove_cpt_slug($args, $post_type) {
if(in_array($post_type, array('car'))) {
$args['rewrite'] = array('slug' => '/');
}
return $args;
}
add_filter('register_post_type_args', 'abc_remove_cpt_slug', 10, 2);