How to Create Taxonomies for WordPress Custom Post Types

Posted OnFeb 3, 2014     CategoryDevelopment     Comments4 comments

In this tutorial we’re going to show you how to create taxonomies for a custom post type.

We’re going to follow on from our previous tutorial where we created the original custom post type. Just like before, you’ll need access to change your WordPress Theme files.

Step 1) Connect to your server and open functions.php in the theme you’re modifying.

Step 2) Create a new function called taxonomies_recipe, substituting the ‘recipe’ portion for whatever your custom post type is.

function taxonomies_recipe() {
 $labels = array(
	'name'              => _x( 'Recipe Categories', 'taxonomy general name' ),
	'singular_name'     => _x( 'Recipe Category', 'taxonomy singular name' ),
	'search_items'      => __( 'Search Recipe Categories' ),
	'all_items'         => __( 'All Recipe Categories' ),
	'parent_item'       => __( 'Parent Recipe Category' ),
	'parent_item_colon' => __( 'Parent Recipe Category:' ),
	'edit_item'         => __( 'Edit Recipe Category' ), 
	'update_item'       => __( 'Update Recipe Category' ),
	'add_new_item'      => __( 'Add New Recipe Category' ),
	'new_item_name'     => __( 'New Recipe Category' ),
	'menu_name'         => __( 'Recipe Categories' ),
	);

Within this function we define the $labels variable and assign an array to it, filled with the required data. It’s much the same information as the $labels variable where we created the custom post type, as it really pertains to the menu we’re creating.

$args = array(
	'labels' => $labels,
	'hierarchical' => true,
	);

Below $labels we create another variable, this time $args, and again assign an array to it, where labels calls in the variable $labels, and we define the taxonomy as hierarchical.

Now it’s time to wrap the function up.

register_taxonomy( 'recipe_category', 'recipe', $args );
}
add_action( 'init', 'taxonomies_recipe', 0 );

We’re using the WordPress function register_taxonomy to make WordPress aware of the taxonomy details via the provided arguments, and then using add_action to handle this on initialization, to make WordPress have this functionality from the get go.

Step 3) Head to the WordPress Dashboard to make sure your changes are live. You should see something just like this. It’s the same Category page that we’re used to, but specific to the custom post type we’ve created.

recipe taxonomy

Here’s the entirety of the code we’ve written in this tutorial for your review.

function taxonomies_recipe() {
$labels = array(
	'name'              => _x( 'Recipe Categories', 'taxonomy general name' ),
	'singular_name'     => _x( 'Recipe Category', 'taxonomy singular name' ),
	'search_items'      => __( 'Search Recipe Categories' ),
	'all_items'         => __( 'All Recipe Categories' ),
	'parent_item'       => __( 'Parent Recipe Category' ),
	'parent_item_colon' => __( 'Parent Recipe Category:' ),
	'edit_item'         => __( 'Edit Recipe Category' ), 
	'update_item'       => __( 'Update Recipe Category' ),
	'add_new_item'      => __( 'Add New Recipe Category' ),
	'new_item_name'     => __( 'New Recipe Category' ),
	'menu_name'         => __( 'Recipe Categories' ),
	);

$args = array(
	'labels' => $labels,
	'hierarchical' => true,
	);

register_taxonomy( 'recipe_category', 'recipe', $args );
}
add_action( 'init', 'taxonomies_recipe', 0 );

4 comments

Comments

  1. Yasir

    Retreiving data in while loop using the below code, but its not retrieving the category name. Please help..

    ‘recipe’,
    ‘order’ => ‘ASC’,
    ‘paged’ => $paged
    );
    $wp_query = new WP_Query( $args );
    $i = 1;
    while ( $wp_query->have_posts()) : $wp_query->the_post();
    $content = $post->post_content;
    ?>

    Works fine
    Works fine
    Not working..???

Leave a Reply

Your email address will not be published. Required fields are marked *

Send this to friend