I have a custom post type :
// Custom posttype Events
$labels = array(
'name' => _x('Events', 'Post Type General Name'),
'singular_name' => _x('Events', 'Post Type Singular Name'),
'menu_name' => __('Events'),
'parent_item_colon' => __('Events:'),
'all_items' => __('All Items'),
'view_item' => __('View Item'),
'add_new_item' => __('Add New Event'),
'add_new' => __('Add New'),
'edit_item' => __('Edit Item'),
'update_item' => __('Update Item'),
'search_items' => __('Search Item'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
'labels' => $labels,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
'taxonomies' => array('post_tag'),
'hierarchical' => false,
'rewrite' => array('slug' => __('events')),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type('events', $args);
And a taxonomy for custom post type Events:
// Add new "Type" taxonomy to Events
register_taxonomy('type-events', 'event', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Types', 'taxonomy general name', 'my_theme' ),
'singular_name' => _x( 'Types', 'taxonomy singular name', 'my_theme' ),
'search_items' => __( 'Search Type', 'my_theme' ),
'all_items' => __( 'All Types', 'my_theme' ),
'parent_item' => __( 'Parent Type', 'my_theme' ),
'parent_item_colon' => __( 'Parent Type:', 'my_theme' ),
'edit_item' => __( 'Edit Type', 'my_theme' ),
'update_item' => __( 'Update Type', 'my_theme' ),
'add_new_item' => __( 'Add New Type', 'my_theme' ),
'new_item_name' => __( 'New Type', 'my_theme' ),
'menu_name' => __( 'Types', 'my_theme' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'type-events',
'with_front' => false,
'hierarchical' => true
),
));
In Dashboard Admin, i create two taxonomy type event :
Taxonomies
Custom post type and taxonomy use the same template.
In file template, I want check if it is post type or taxonomy.
Currently, I use is_post_type_archive()
to check, but the both return true. That is not what I need.
How to check if this is custom post type or taxonomy?
If you want to check if a post is the custom post type
events
within the loop you can use this:If this is outside of the loop, you need to pass the post id into
get_post_type()
:Edit
You can test for multiple custom post types this way:
This is little complicated, maybe the best way is to check with
is_post_type_archive()
andis_tax()
, before you load the themplate, but in your case you may try with get_queried_object().Codex – get_queried_object
The returned object will have properties
taxonomy
,term_id
, andterm_taxonomy_id
if this is taxonomy.So maybe you can use something like
get_queried_object()->taxonomy
to determine if this is taxonomy and get its name andget_queried_object()->query_var
for custom_post_type.