jeudi 26 mars 2015

Custom post type, permalinks, taxonomies and blog posts


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/


http://ift.tt/1IAZKwm


http://ift.tt/1bzgqJR


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.





Aucun commentaire:

Enregistrer un commentaire