I have created custom post type ‘Portfolio’ also under this post type I have created Portfolio categories with following code
functions.php – following is the code for custom post type i have defined in functions.php page
function demo_register_post_type() {
register_post_type('Portfolio', array(
'labels' => array(
'name' => __('Portfolio'),
'singular_name' => __('Portfolio'),
'add_new' => 'Add New Portfolio',
'edit_item' => 'Edit Portfolio',
'new_item' => 'New Portfolio',
'view_item' => 'View Portfolio',
'search_items' => 'Search Portfolio',
'not_found' => 'No Portfolio found',
'not_found_in_trash' => 'No Portfolio found in Trash',
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'query_var' => true
),
'menu_position' => 6,
'public' => true,
'supports' => array(
'title','editor','author','thumbnail','excerpt','comments','page-attributes'
),
'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
'taxonomy' => array('category', 'post_tag')
));
register_taxonomy( 'portfolio-category', 'Portfolio', array ('hierarchical' => true, 'label' => __('Portfolio Categories'))); // portfolio categories
Now I want to retrieve all the categories from this post type and display posts from the categories on page?
Please let me know how could I do this?
Thanks.
What about using get_terms()?
Quick example:
to get the posts of your custom post type you need to query post_type and you can do it like so:
Now if you want to get Portfolio post of a specific term in your custom taxonamy
then add the taxonomy argument to the query_posts array like so:
Hope this helps.
Try this code, it’s work. Thank me later.