Search code examples
wordpresscustom-post-typepermalinks

Wordpress CPT multiple categories in the url


I have a CPT Traffi coffice. Url: www.domain.com/traffic-offices/

When add a new like www.domain.com/traffic-offices/germany/ its ok. When i add a new post Saksen then it automaticly makes this url www.domain.com/traffic-offices/saksen but i want www.domain.com/traffic-offices/germany/saksen. If i fill that in it automaticly makes www.domain.com/traffic-offices/germany-saksen.

How can i achieve this url www.domain.com/traffic-offices/germany/saksen ?

    function trafficoffice_post_type() {

    // Labels
    $labels = array(
        'name' => _x("trafficoffices", "post type general name"),
        'singular_name' => _x("trafficoffices", "post type singular name"),
        'menu_name' => 'trafficoffices',
        'add_new' => _x("Add new", "trafficoffice item"),
        'add_new_item' => __("Add new"),
        'edit_item' => __("trafficoffice aanpassen"),
        'new_item' => __("Add new trafficoffice"),
        'view_item' => __("View trafficoffice"),
        'search_items' => __("Search Profiles"),
        'not_found' => __("No Profiles Found"),
        'not_found_in_trash' => __("No Profiles Found in Trash"),
        'parent_item_colon' => ''
    );

    // Register post type
    register_post_type('trafficoffice', array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => false,
        'rewrite'     => array(
            'slug' => 'traffic-offices',
        ),
        'supports' => array('title', 'editor', 'thumbnail')
    ));
}

add_action('init', 'trafficoffice_post_type', 0);

Solution

  • One way of doing that is using taxonomies.

    1. Register a new taxonomy called "country".
    2. Create a new category "Germany" with slug "germany".
    3. Assign the category "Germany" to the post "Saksen".

    Here is the code to register the taxonomy:

    $labels = array(
        'name'                       => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
        'singular_name'              => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
        'search_items'               => __( 'Search Countries', 'textdomain' ),
        'popular_items'              => __( 'Popular Countries', 'textdomain' ),
        'all_items'                  => __( 'All Countries', 'textdomain' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit Country', 'textdomain' ),
        'update_item'                => __( 'Update Country', 'textdomain' ),
        'add_new_item'               => __( 'Add New Country', 'textdomain' ),
        'new_item_name'              => __( 'New Country Name', 'textdomain' ),
        'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
        'add_or_remove_items'        => __( 'Add or remove Countries', 'textdomain' ),
        'choose_from_most_used'      => __( 'Choose from the most used Countries', 'textdomain' ),
        'not_found'                  => __( 'No Countries found.', 'textdomain' ),
        'menu_name'                  => __( 'Countries', 'textdomain' ),
    );
    
    $args = array(
        'hierarchical'          => false,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        //'rewrite'               => array( 'slug' => 'country' ),
    );
    
    register_taxonomy( 'country', 'trafficoffice', $args );
    

    Make sure the permalink settings of WordPress (Settings -> Permalinks) include the category.

    /%category%/%postname%/
    

    Read more about register_taxonomy().