1

I have a custom post type to list countries but I am not sure how to create them with this permalink structure:

  • Country archive: /map
  • Country single: /map/country/england

How can I achieve this? Heres my country post code:

function countries() {

    $labels = array(
        'name'                  => _x( 'countries', 'Post Type General Name', 'countries' ),
        'singular_name'         => _x( 'country', 'Post Type Singular Name', 'countries' ),
        'menu_name'             => __( 'Countries', 'countries' ),
        'name_admin_bar'        => __( 'Country', 'countries' ),
        'parent_item_colon'     => __( 'Parent Country', 'countries' ),
        'all_items'             => __( 'All Countries', 'countries' ),
        'add_new_item'          => __( 'Add New Country', 'countries' ),
        'add_new'               => __( 'Add New Country', 'countries' ),
        'new_item'              => __( 'New Country', 'countries' ),
        'edit_item'             => __( 'Edit Country', 'countries' ),
        'update_item'           => __( 'Update Country', 'countries' ),
        'view_item'             => __( 'View Country', 'countries' ),
        'search_items'          => __( 'Search Countries', 'countries' ),
        'not_found'             => __( 'Country Not found', 'countries' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'countries' ),
        'items_list'            => __( 'Country list', 'countries' ),
        'items_list_navigation' => __( 'Countries List Navigation', 'countries' ),
        'filter_items_list'     => __( 'Filter Country List', 'countries' ),
    );
    $rewrite = array(
        'slug'                  => 'country',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => __( 'country', 'countries' ),
        'description'           => __( 'Country Information', 'countries' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
    );
    register_post_type( 'countries', $args );

}
add_action( 'init', 'countries', 0 );

I am also trying this with the following taxonomy but it 404s even when I update the permalink settings page:

register_taxonomy(
    'region',
    'country',
    [
        'label' => __( 'Region' ),
        'rewrite' => array( 'slug' => 'map/region' ),
        'hierarchical' => true,
    ]
);
3

To get your desired archive URL, set has_archive to 'map' instead of true.

To get your desired single post URL, set the slug argument in your $rewrite array to 'map/country'

  • Ok thankyou. I have also added a tax called region and it's URL is /region/europe is there a way it can be appended to the map URL? So like /map/regoin/europe – Callum Bonnyman Nov 17 '15 at 5:48
  • same as the post type, make the rewrite slug 'map/region'. – Milo Nov 17 '15 at 6:27
  • I have tried this and it's not working for me. Can you see the edit I made at the end of the original post? – Callum Bonnyman Nov 17 '15 at 10:31
  • what do you mean by not working? what happens? did you flush permalinks after making changes? also note that your post type is countries, the 2nd argument of register_taxonomy should also be countries. – Milo Nov 17 '15 at 16:13
1

You just want a hierarchical post type. Change 'hierarchical' => false, to 'hierarchical' => true, and nest your posts appropriately

  • Ok thankyou. I have also added a tax called region and it's URL is /region/europe is there a way it can be appended to the map URL? So like /map/regoin/europe – Callum Bonnyman Nov 17 '15 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.