带自定义标签阵列的Wordpress自定义帖子类型

时间:2014-07-24 12:04:21

标签: php wordpress wordpress-plugin wordpress-theming

我正在尝试使用自己的类别类型和标记类型创建自定义帖子类型。我有自定义类别,但我在设置自定义标记数组时遇到了一些困难。

我基本上想要为帖子类型设置标签,因此它们被称为服务。

这是我到目前为止所做的,但它会产生错误并杀死实例。

<?php
// Adding Custom Post Type : Project
add_action('init', 'project_register');
function project_register() {
    $labels = array(
    'name'              => _x('Projects','Projects','Projects'),
    'singular_name'         => _x('Project','Project','Project'),
    'add_new'           => _x('Add New Project','Project Listing','Project'),
    'add_new_item'          => __('Add New Project','Project'),
    'edit_item'         => __('Edit Project','Project'),
    'new_item'          => __('New Project Post Item','Project'),
    'view_item'         => __('View Project Item','Project'),
    'search_items'          => __('Search Project','Project'),
    'not_found'         => __('Nothing found','Project'),
    'not_found_in_trash'        => __('Nothing found in Trash','Project'),
    'parent_item_colon'     => ''
    );

    $args = array(
    'labels'            => $labels,
    'public'            => true,
    'menu_position'         => 5,
    'exclude_from_search'       => true,
    'show_ui'           => true,
    'capability_type'       => 'post',
    'show_in_nav_menus'         => false,
    'hierarchical'          => false,
    'rewrite'           => array( 'with_front' => false ),
    'query_var'         => true,            
    'supports'          => array('title', 'editor', 'author', 'excerpt', 'thumbnail', 'comments'),
    'has_archive'           => true,
    'taxonomies'            => $tags,
); 
register_post_type( 'project' , $args ); 

    // Initialize New Taxonomy Labels  
    $labels = array(  
    'name'          => _x( 'Categories', 'taxonomy general name' ),  
    'singular_name'     => _x( 'Category', 'taxonomy singular name' ),  
    'search_items'      => __( 'Search Categories' ),  
    'all_items'         => __( 'All Categories' ),  
    'parent_item'       => __( 'Parent Category' ),  
    'parent_item_colon'     => __( 'Parent Category:' ),  
    'edit_item'         => __( 'Edit Categories' ),  
    'update_item'       => __( 'Update Category' ),  
    'add_new_item'      => __( 'Add New Category' ),  
    'new_item_name'     => __( 'New Category Name' ),  
    );  

   // Initialize New Taxonomy Tags
    $tags = array(  
    'name'          => _x( 'Services', 'taxonomy general name' ),  
    'singular_name'     => _x( 'Service', 'taxonomy singular name' ),  
    'search_items'      => __( 'Search Services' ),  
    'all_items'         => __( 'All Services' ),  
    'parent_item'       => __( 'Parent Service' ),  
    'parent_item_colon'     => __( 'Parent Service:' ),  
    'edit_item'         => __( 'Edit Services' ),  
    'update_item'       => __( 'Update Service' ),  
    'add_new_item'      => __( 'Add New Service' ),  
    'new_item_name'     => __( 'New Service Name' ),  
    ); 

    // Custom taxonomy for project categories 
    register_taxonomy('category-project', array('project'), array(  
    'hierarchical'      => true,  
    'labels'        => $labels,  
    'show_ui'       => true,  
    'query_var'         => true,  
    'rewrite'       => array( 'slug' => 'category-project' ),  
    )); 

    // Custom taxonomy for project categories 
    register_taxonomy('service-project', array('project'), array(  
    'hierarchical'      => true,  
    'labels'        => $tags,  
    'show_ui'       => true,  
    'query_var'         => true,  
    'rewrite'       => array( 'slug' => 'service-project' ),  
    ));
}


?>

1 个答案:

答案 0 :(得分:1)

您的代码有一些错误。请在wp-config

中启用调试

我在这里看到的一两个大问题是:

  • 您在帖子类型参数和上一个分类中使用了未定义的变量$tags。你在哪里设置变量?它也应该在你的功能中,而不是在

  • 之外
  • 您有两个$labels实例,因此第一个分类标准混淆了哪个使用和失败。

  • 如果您想将分类法用作代码,请将hierarchial设置为falsetrue会使分类法表现为类别

修改

这是您的代码,应该按原样运行

add_action('init', 'project_register');
function project_register() {
    $labels = array(
    'name'              => _x('Projects','Projects','Projects'),
    'singular_name'         => _x('Project','Project','Project'),
    'add_new'           => _x('Add New Project','Project Listing','Project'),
    'add_new_item'          => __('Add New Project','Project'),
    'edit_item'         => __('Edit Project','Project'),
    'new_item'          => __('New Project Post Item','Project'),
    'view_item'         => __('View Project Item','Project'),
    'search_items'          => __('Search Project','Project'),
    'not_found'         => __('Nothing found','Project'),
    'not_found_in_trash'        => __('Nothing found in Trash','Project'),
    'parent_item_colon'     => ''
    );

    $args = array(
    'labels'            => $labels,
    'public'            => true,
    'menu_position'         => 5,
    'exclude_from_search'       => true,
    'show_ui'           => true,
    'capability_type'       => 'post',
    'show_in_nav_menus'         => false,
    'hierarchical'          => false,
    'rewrite'           => array( 'with_front' => false ),
    'query_var'         => true,            
    'supports'          => array('title', 'editor', 'author', 'excerpt', 'thumbnail', 'comments'),
    'has_archive'           => true,
    'taxonomies'            => $tags,
); 
register_post_type( 'project' , $args ); 

    // Initialize New Taxonomy Labels  
    $labels = array(  
    'name'          => _x( 'Categories', 'taxonomy general name' ),  
    'singular_name'     => _x( 'Category', 'taxonomy singular name' ),  
    'search_items'      => __( 'Search Categories' ),  
    'all_items'         => __( 'All Categories' ),  
    'parent_item'       => __( 'Parent Category' ),  
    'parent_item_colon'     => __( 'Parent Category:' ),  
    'edit_item'         => __( 'Edit Categories' ),  
    'update_item'       => __( 'Update Category' ),  
    'add_new_item'      => __( 'Add New Category' ),  
    'new_item_name'     => __( 'New Category Name' ),  
    );  

   // Initialize New Taxonomy Tags
    $tags = array(  
    'name'          => _x( 'Services', 'taxonomy general name' ),  
    'singular_name'     => _x( 'Service', 'taxonomy singular name' ),  
    'search_items'      => __( 'Search Services' ),  
    'all_items'         => __( 'All Services' ),  
    'parent_item'       => __( 'Parent Service' ),  
    'parent_item_colon'     => __( 'Parent Service:' ),  
    'edit_item'         => __( 'Edit Services' ),  
    'update_item'       => __( 'Update Service' ),  
    'add_new_item'      => __( 'Add New Service' ),  
    'new_item_name'     => __( 'New Service Name' ),  
    ); 

    // Custom taxonomy for project categories 
    register_taxonomy('category-project', array('project'), array(  
    'hierarchical'      => true,  
    'labels'        => $labels,  
    'show_ui'       => true,  
    'query_var'         => true,  
    'rewrite'       => array( 'slug' => 'category-project' ),  
    )); 

    // Custom taxonomy for project categories 
    register_taxonomy('service-project', array('project'), array(  
    'hierarchical'      => false,  
    'labels'        => $tags,  
    'show_ui'       => true,  
    'query_var'         => true,  
    'rewrite'       => array( 'slug' => 'service-project' ),  
    ));
}