• We are available for any Development work:
  • Email us at: programmer_heart@yahoo.com

Get related Categories of a post in cstom post type

You can use following code for get all related  categories of any post type but you need to keep in mind that post type must support the categories taxonomy . You  can make the custom post type support categories while registering the custom post type.


$termcat=$post_type.'_category';
$category = get_the_terms($post->ID, $termcat);
foreach ( $category as $cat){
$categories[]=$cat->name;
}

 

You can add following code  to enable the categories of  custom post type . below example adds the scenes category of post type SCENES.and taxonomy for the post type is  scenes_category.in normal wordpress post type post  its  the taxonomy for category is only category.


function my_taxonomies_Scene() {
$labels = array(
'name'              => _x( 'Scene Categories', 'taxonomy general name' ),
'singular_name'     => _x( 'Scene Category', 'taxonomy singular name' ),
'search_items'      => __( 'Search Scene Categories' ),
'all_items'         => __( 'All Scene Categories' ),
'parent_item'       => __( 'Parent Scene Category' ),
'parent_item_colon' => __( 'Parent Scene Category:' ),
'edit_item'         => __( 'Edit Scene Category' ),
'update_item'       => __( 'Update Scene Category' ),
'add_new_item'      => __( 'Add New Scene Category' ),
'new_item_name'     => __( 'New Scene Category' ),
'menu_name'         => __( 'Scene Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'scenes_category', 'scenes', $args );
flush_rewrite_rules();
}
add_action( 'init', 'my_taxonomies_Scene', 0 );

One thought on “Get related Categories of a post in cstom post type

  1. Jolene says:

    hi!,I like your writing very much! proportion we keep up a correspondence extra approximately your article on AOL? I require an expert in this area to solve my problem. Maybe that is you! Looking forward to see you.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *

Press Enter to Search