Support » How-To and Troubleshooting » [Resolved] Add category to a custom post type?

[Resolved] Add category to a custom post type?

  • Hi Guys,
    I have been reading other posts for hours but cannot find an answer.

    Basically I have a ‘review’ theme that is a custom post type, I cannot specify any categories for this post type that I create in posts>categories, I want to be able to use this function.

    The reason for this is that I have a slider on the homepage that will only display posts from standard categories that I define in posts>categories, I want the most recent reviews to be able to be shown in the slider by having a category called ‘Featured’ (for example.

    Is this possible? at first I thought custom fields may work by using something like ‘category – “Horror”‘ but I dont even have access to custom fields from within the custom post type editor (see screenshot).

    Screenshot – http://www.phillshaw.co.uk/babylongeek/wp-content/uploads/2012/07/Screen-Shot-2012-07-20-at-19.21.24.jpg

    I have thought about plugins but i’m not really sure what I am supposed to be doing. I have also contacted the developer but haven’t had a reply, yet.

    The theme is ‘Valor’ – http://www.web2feel.com/valor/
    The review post type DOES have a custom category selector called consoles but the slider cannot use these.

    Any thoughts would be amazing!

Viewing 15 replies - 1 through 15 (of 24 total)
  • You can add the regular post category taxonomy to a custom post type in its register_post_type statement, sample below:

        'labels' => array(
                 //your label stuff
      'taxonomies' => array('category'),       <========= add this....
      'public' => true,
      'show_ui' => true,
      'exclude_from_search' => true,
      'hierarchical' => true,
      'supports' => array( 'title', 'editor', 'thumbnail' ),
      'query_var' => true

    where would I add that?
    in the single-review.php (which is the cutom post type) my code is:

    // Review Post type
    add_action('init', 'review_register');
    function review_register() {
    	$labels = array(
    		'name' => _x('Reviews', 'post type general name'),
    		'singular_name' => _x('Review', 'post type singular name'),
    		'add_new' => _x('Add New', 'review'),
    		'add_new_item' => __('Add New Review'),
    		'edit_item' => __('Edit Review'),
    		'new_item' => __('New Review'),
    		'view_item' => __('View Review'),
    		'search_items' => __('Search Review'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => null,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail')
    	register_post_type( 'review' , $args );
    // Custom Taxonomy
    function add_console_taxonomies() {
    	register_taxonomy('console', 'review', array(
    		// Hierarchical taxonomy (like categories)
    		'hierarchical' => true,
    		// This array of options controls the labels displayed in the WordPress Admin UI
    		'labels' => array(
    			'name' => _x( 'Review Category', 'taxonomy general name' ),
    			'singular_name' => _x( 'Review-Category', 'taxonomy singular name' ),
    			'search_items' =>  __( 'Search Review-Categories' ),
    			'all_items' => __( 'All Review-Categories' ),
    			'parent_item' => __( 'Parent Review-Category' ),
    			'parent_item_colon' => __( 'Parent Review-Category:' ),
    			'edit_item' => __( 'Edit Review-Category' ),
    			'update_item' => __( 'Update Review-Category' ),
    			'add_new_item' => __( 'Add New Review-Category' ),
    			'new_item_name' => __( 'New Review-Category Name' ),
    			'menu_name' => __( 'Review Categories' ),
    		// Control the slugs used for this taxonomy
    		'rewrite' => array(
    			'slug' => 'console', // This controls the base slug that will display before each term
    			'with_front' => false, // Don't display the category base before "/locations/"
    			'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    add_action( 'init', 'add_console_taxonomies', 0 );

    see code below

    $args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => null,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail'),
                    'taxonomies' => array('category')

    ok I made the changes and:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in /websites/123reg/LinuxPackage21/ph/il/ls/phillshaw.co.uk/public_html/babylongeek/wp-content/themes/Valor/cpt.php on line 35

    My eyes are not noticing any typo in what I posted.

    Your original post said the code you posted was from file single-review.php

    The error message says the error is in theme file cpt.php

    Not sure why changing something in single-review.php would cause a syntax error in cpt.php.

    Does the error go away when you revert the $args code back to what it was before my suggestion?

    Sorry my mistake in the first post, its not single-review.php its cpt.php where the code you suggested lives.

    Yes when I take out
    'taxonomies' => array('category')
    the site returns to normal.

    try this instead.
    in cpt.php after
    register_post_type( 'review' , $args );

    register_taxonomy_for_object_type( 'category', 'review' );

    Hi. PhillShaw.

    This code might help you. Have a look.

    function themes_taxonomy() {
    		'themes_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
    		'themes',   		 //post type name
    			'hierarchical' 		=> true,
    			'label' 			=> 'Themes store',  //Display name
    			'query_var' 		=> true,
    			'rewrite'			=> array(
    					'slug' 			=> 'themes', // This controls the base slug that will display before each term
    					'with_front' 	=> false // Don't display the category base before
    add_action( 'init', 'themes_taxonomy');'
     * Maintain the permalink structure for custom taxonomy
     * Display custom taxonomy term name before post related to that term
     * @uses post_type_filter hook
    'function filter_post_type_link( $link, $post) {
        if ( $post->post_type != 'themes' )
            return $link;
        if ( $cats = get_the_terms( $post->ID, 'themes_categories' ) )
            $link = str_replace( '%themes_categories%', array_pop($cats)->slug, $link );
        return $link;
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);'
    //Registering Custom Post Type Themes
    'add_action( 'init', 'register_themepost', 20 );
    function register_themepost() {
        $labels = array(
    		'name' => _x( 'Themes', 'catchthemes_custom_post','catchthemes' ),
    		'singular_name' => _x( 'Theme', 'catchthemes_custom_post', 'catchthemes' ),
    		'add_new' => _x( 'Add New', 'catchthemes_custom_post', 'catchthemes' ),
    		'add_new_item' => _x( 'Add New ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'edit_item' => _x( 'Edit ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'new_item' => _x( 'New ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'view_item' => _x( 'View ThemePost', 'catchthemes_custom_post', 'catchthemes' ),
    		'search_items' => _x( 'Search ThemePosts', 'catchthemes_custom_post', 'catchthemes' ),
    		'not_found' => _x( 'No ThemePosts found', 'catchthemes_custom_post', 'catchthemes' ),
    		'not_found_in_trash' => _x( 'No ThemePosts found in Trash', 'catchthemes_custom_post', 'catchthemes' ),
    		'parent_item_colon' => _x( 'Parent ThemePost:', 'catchthemes_custom_post', 'catchthemes' ),
    		'menu_name' => _x( 'Themes Posts', 'catchthemes_custom_post', 'catchthemes' ),
        $args = array(
    		'labels' => $labels,
    		'hierarchical' => false,
    		'description' => 'Custom Theme Posts',
    		'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
    		'taxonomies' => array( 'post_tag','themes_categories'),
    		'show_ui' => true,
    		'show_in_menu' => true,
    		'menu_position' => 5,
    		'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
    		'show_in_nav_menus' => true,
    		'publicly_queryable' => true,
    		'exclude_from_search' => false,
    		'query_var' => true,
    		'can_export' => true,
    		'rewrite' => array('slug' => 'themes/%themes_categories%','with_front' => FALSE),
    		'public' => true,
    		'has_archive' => 'themes',
    		'capability_type' => 'post'
    	register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces

    [moderated: please ensure that your code is enclosed in backticks (`) or use the code button.]

    at stvwlf, That solved it! Much appreciated for your efforts!!

    Thanks guys.

    I spoke too soon!
    I now have access to the categories from within custom post type but on the frontend of the site it doesnt look like they’re being filtered through, I have added ‘Movie News’ as a category within my review (custom post type) but when I click movie news on my site it isnt there??

    I suspect a normal WordPress category link is only going to display posts of post type ‘posts’. You probably need to filter the category list code for it to include posts from other post types.

    A quick search brought up this
    See if it helps or gets you started

    Ok thanks for that, it makes sense in that I need to call those categories but the code makes no sense to me!

    Any ideas on how to make this function for my situation? (sorry to keep leeching your time!)

    but when I click movie news on my site it isnt there??

    Please post the relevant PHP code that is displaying the category links that are not including the CPT’s. If its more than ten lines, post it in a pastebin http://pastebin.com/ and link the pastebin URL here.

    I have to see how the category links are being created (the underlying PHP code) to give suggestions on how to include custom post types in the cat pages.

    ok thanks,

    here is the full cpt.php code:


    That is the Custom post type code. (We already have the CPT and Taxonomy working correctly)

    I need to see the code that displays the Movie News link – what is the URL of the page that has the Movie News Link on it? The one that when clicked doesn’t display the CPT Reviews posts. It is the code that displays that link that I need to see.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘[Resolved] Add category to a custom post type?’ is closed to new replies.
Skip to toolbar