• 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.

[code]

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

[/code]

 

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.

[code]

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 );

[/code]

Leave a Reply

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

Press Enter to Search