2017-02-13 70 views
2

点点帮助需要我的这个代码我的自定义后类型名称。我现在面临这个问题大约2天,但无法弄清楚。为什么我没有收到与get_post_type()

我想让我的自定义帖子类型名称,我注册。

$type = get_post_type(); 
     echo "<h1>Post type is $type</h1>"; 
     if(get_query_var('post_type') !== 'jobs'){ 

      return $original_template; 

     } 

我如何注册我的自定义后类型低于:

function dwwp_register_post_type() { 

    $singular = __('Job'); 
    $plural = __('Jobs'); 
     //Used for the rewrite slug below. 
     $plural_slug = str_replace(' ', '_', $plural); 

     //Setup all the labels to accurately reflect this post type. 
    $labels = array(
     'name'     => $plural, 
     'singular_name'   => $singular, 
     'add_new'    => 'Add New', 
     'add_new_item'   => 'Add New ' . $singular, 
     'edit'     => 'Edit', 
     'edit_item'    => 'Edit ' . $singular, 
     'new_item'    => 'New ' . $singular, 
     'view'     => 'View ' . $singular, 
     'view_item'    => 'View ' . $singular, 
     'search_term'   => 'Search ' . $plural, 
     'parent'    => 'Parent ' . $singular, 
     'not_found'    => 'No ' . $plural .' found', 
     'not_found_in_trash' => 'No ' . $plural .' in Trash' 
    ); 

     //Define all the arguments for this post type. 
    $args = array(
     'labels'    => $labels, 
     'public'    => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'show_in_nav_menus' => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'show_in_admin_bar' => true, 
     'menu_position'  => 6, 
     'menu_icon'   => 'dashicons-admin-site', 
     'can_export'   => true, 
     'delete_with_user' => false, 
     'hierarchical'  => true, 
     'has_archive'   => true, 
     'query_var'   => true, 
     'capability_type'  => 'post', 
     'map_meta_cap'  => true, 
     // 'capabilities' => array(), 
     'rewrite'    => array( 
      'slug' => 'jobs', 
      'with_front' => false, 
     ), 
     'supports'   => array( 
      'title' 
     ) 
    ); 

     //Create the post type using the above two varaiables. 
    register_post_type('jobs', $args); 
} 

回答

0
$get_cpt_args = array(
'public' => true, 
'_builtin' => false 
); 

$post_types = get_post_types($get_cpt_args, 'object'); 
if(($post_types) !== 'jobs') { 

    echo "Post type is not a jobs"; 
} else { 

    echo "Post type is jobs"; 
} 

与您的自定义后类型的尝试。这可能会帮助你。

相关问题