检查是否是自定义帖子类型

Mít*_*Mít 1 wordpress

我有一个自定义帖子类型:

// 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);
Run Code Online (Sandbox Code Playgroud)

以及自定义帖子类型事件的分类:

// 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
        ),
    ));
Run Code Online (Sandbox Code Playgroud)

在仪表板管理中,我创建了两个分类类型事件: 分类

自定义帖子类型和分类使用相同的模板。

在文件模板中,我想检查它是帖子类型还是分类法。 目前,我用来is_post_type_archive()检查,但两者都返回 true。那不是我需要的。

如何检查这是否是自定义帖子类型或分类法?

Mar*_*arc 6

如果您想检查某个帖子是否是events循环内的自定义帖子类型,您可以使用以下命令:

<?php if ( get_post_type() === 'events' ) { 
    /* Do Stuff */ 
} ?>
Run Code Online (Sandbox Code Playgroud)

如果这是在循环之外,则需要将帖子 id 传递到get_post_type()

<?php if ( get_post_type( $post_id ) === 'events' ) { 
    /* Do Stuff */ 
} ?>
Run Code Online (Sandbox Code Playgroud)

编辑

您可以通过以下方式测试多种自定义帖子类型:

<?php if ( get_post_type() === 'events' || get_post_type() === 'promos' || get_post_type() === 'courses' ) { 
    /* Do Stuff */ 
} ?>
Run Code Online (Sandbox Code Playgroud)