Pages, Custom Posts & Custom Taxonomy defining slug structure

All of the cases you describe are handled by the post type archive, post type single, and taxonomy term archive pages that are all automatically generated (depending on arguments) when you register your post type and taxonomy.

function wpd_add_custom_types() {

    // register fruit post type
    $args = array(
        'public' => true,
        'label'  => 'Fruit',
        'has_archive' => 'fruits', // all posts of type fruit
        'rewrite' => array( 'slug' => 'fruit' ) // single fruit
    );
    register_post_type( 'fruit', $args );

    // register colour taxonomy
    $args = array(
        'label' => 'Colour',
        'rewrite' => array( 'slug' => 'fruits/colour' ) // all fruits of colour
    );
    register_taxonomy( 'colour', 'fruit', $args );

}
add_action( 'init', 'wpd_add_custom_types' );

Leave a Comment