1

I am creating a custom theme for a bike shop and thought having a custom post type for bikes would be easier for the shop assistant to manage the bikes they would like to appear on their website rather than have a huge amount of child pages.

To start things off, here is my code for the bikes custom post type in functions.php

/*
* Register `bikes` post type
*/
function bikes_post_type() {

   // Labels
    $labels = array(
        'name' => _x("Bikes", "post type general name"),
        'singular_name' => _x("Bike", "post type singular name"),
        'menu_name' => 'Bikes',
        'add_new' => _x("Add New", "bike item"),
        'add_new_item' => __("Add New Bike"),
        'edit_item' => __("Edit Bike"),
        'new_item' => __("New Bike"),
        'view_item' => __("View Bike"),
        'search_items' => __("Search Bikes"),
        'not_found' =>  __("No Bikes Found"),
        'not_found_in_trash' => __("No Bikes Found in Trash"),
        'parent_item_colon' => ''
    );

    // Register post type
    register_post_type('bikes' , array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => false,
        'rewrite' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/images/icon-bikes.png',
        'supports' => array('title', 'editor', 'thumbnail'),
        //'taxonomies' => array( 'category', 'post_tag' ),
        //'rewrite' => array('slug' => 'bikes', 'with_front' => false,)
    ) );
}
add_action( 'init', 'bikes_post_type' );


/*
 * Register `bikes` taxonomy
*/
function bikes_taxonomy() {

    // Labels
    $singular = 'Bikes Category';
    $plural = 'Bikes Categories';
    $labels = array(
        'name' => _x( $plural, "taxonomy general name"),
        'singular_name' => _x( $singular, "taxonomy singular name"),
        'search_items' =>  __("Search $singular"),
        'all_items' => __("All $singular"),
        'parent_item' => __("Parent $singular"),
        'parent_item_colon' => __("Parent $singular:"),
        'edit_item' => __("Edit $singular"),
        'update_item' => __("Update $singular"),
        'add_new_item' => __("Add New $singular"),
        'new_item_name' => __("New $singular Name"),
    );

    // Register and attach to 'bikes' post type
    register_taxonomy( 'bikes_category', 'bikes', array(
        'public' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'hierarchical' => true,
        'query_var' => true,
        'labels' => $labels,
        //'rewrite' => array('slug' => 'bikes', 'with_front' => false),
    ) );
}
add_action( 'init', 'bikes_taxonomy');

The commented out lines at the end of the register_post_type() and register_taxonomy() functions are causing a problem for me.

The permalink structure I have set is /blog/%year%/%monthnum%/%postname%/ as that is the way I would like the blog posts to be structured.

The page hierarchy is set up like this: domain.com/bikes/

domain.com/bikes/mountain-bikes/

domain.com/bikes/road-bikes/

The /bikes/ page will be a landing page of sorts with some text about bikes in general as well as a sentence or two regarding mountain bikes and road bikes with links to the child landing pages /bikes/mountain-bikes/ and /bikes/road-bikes/.

When I create a new bike, the permalink is /blog/bikes/giant-anthem-x-29er/. I would like the permalink to be something like /bikes/giant-anthem-x-29er/ or /bikes/mountain/giant-anthem-x-29er/. I managed to get /bikes/giant-anthem-x-29er/ to work by changing the permalink setting to /%year%/%monthnum%/%postname%/ but that gave me a 404 error for /bikes/mountain-bikes/ and a permalink for the blog posts without 'blog', which is not what I would like to happen.

I've googled and searched through this site but I can't seem to find anything that will allow me to have an architecture like this:

/bikes/ - standard page template

/bikes/mountain-bikes/ - standard page template

/bikes/road-bikes/ - standard page template

/bikes/mountain-bikes/ - standard page template

/blog/ - default template

/blog/%year%/%monthnum%/%post-name%/ - single post template

/bikes/giant-anthem-x-29er/ - custom bike template (with example slug from the custom post type)

or

/bikes/mountain/giant-anthem-x-29er/ - custom bike template (with taxonomy in the permalink)

Any help in achieving this would be greatly appreciated.

2
  • 1
    You could save yourself a ton of headache and use bike (singular) as the slug for the individual bike CPT singles (/bike/giant-anthem-x-29er/). Otherwise, prepare for rewrite hell.
    – cfx
    Commented Mar 27, 2015 at 2:33
  • have a look at this answer
    – Milo
    Commented Mar 27, 2015 at 4:42

1 Answer 1

0

I would do as @cfx suggested:

register_taxonomy( // Call before register_post_type() to give rewrite rule priority.
    'bike_category', // Should be singular, not bikes_category
    'bike',
    array(
        'rewrite' => array(
            'with_front' => false, // This will strip the "blog" prefix 
            'slug' => 'bikes',
        ),

        // Other args
    )   
);

register_post_type(
    'bike', // Post type should be singular
    array(
        'has_archive' => 'bikes',
        'rewrite' => array(
            'with_front' => false, // This will strip the "blog" prefix 
            'slug' => 'bike',
        ),

        // Other args
    )
);

This will give you:

  • /bikes/ for global bikes archive (archive-bike.php)
  • /bikes/category for a bikes category archive (taxonomy-bike_category.php)
  • /bike/bike-name for a single bike (single-bike.php)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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