views:

27

answers:

2

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.

I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.

+1  A: 

Not a problem at all. When you register the post type, just add this argument to the array:

'taxonomies' => array( 'category', 'post_tag' )
John P Bloch
Awesome! That worked perfectly.
trnsfrmr
A: 

Suppose you defined your cpt (custom post type) by the following:

register_post_type('listings', $args); // where $args is an array of your cpt settings

Then you could use the following to add taxonomy:

// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));

// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);

In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).

The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).

Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.

PeterWong
PeterWong, Re: not using functions.php: How does one create a theme with CPTs that others can easily use without having to use additional plugins and/or get there hands dirty with code?
trnsfrmr
No, it is not aimed for distribution. It is for your own convenient. Think you want to have a whole new looking. You then have to redesign everything. And you have to open your old functions.php and copy your CPTs definitions to your new functions.php. Why doing copy and paste? Putting them into a self-owned plugin reduced your works actually! Those CPTs would stay in your backend even if you switched to other theme. Of course customizing the front end is a must.
PeterWong
I don't really understand what you're suggesting. I'm quite happy with creating CPTs on a theme by theme basis. Thanks.
trnsfrmr
Never mind. It's just programming style.
PeterWong