当前位置: 动力学知识库 > 问答 > 编程问答 >

wordpress - insert taxonomy to a custom post type

问题描述:

I have been created a custom post type and create a taxonomy for it, below is the code which i used to create those:

//create custom post type of jobs

add_action( 'init', 'create_jobs' );

function create_jobs() {

register_post_type( 'jobs',

array(

'labels' => array(

'name' => 'jobs',

'singular_name' => 'Jobs',

'add_new' => 'Add New',

'add_new_item' => 'Add New Jobs',

'edit' => 'Edit',

'edit_item' => 'Edit Jobs',

'new_item' => 'New Jobs',

'view' => 'View',

'view_item' => 'View Jobs',

'search_items' => 'Search Jobs',

'not_found' => 'No Jobs found',

'not_found_in_trash' => 'No Jobs found in Trash',

'parent' => 'Parent Jobs'

),

'public' => true,

'menu_position' => 15,

'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),

'taxonomies' => array( '' ),

'menu_icon' => 'dashicons-visibility',

'has_archive' => true

)

);

}

//create a taxonomy for jobs

add_action( 'init', 'create_jobstax', 0 );

function create_jobstax() {

register_taxonomy(

'jobs_taxonomy_genre',

'jobs',

array(

'labels' => array(

'name' => 'Create Jobs Genre',

'add_new_item' => 'Add New jobs genre',

'new_item_name' => "New jobs genre"

),

'show_ui' => true,

'show_tagcloud' => false,

'hierarchical' => true

)

);

}

now what i want is to insert a default taxonomy for that custom post type only, example: i want to insert "latest" and "active" as a default taxonomy for the jobs custom post type, how to achieve that? currently looking for a approach around but seems found nothing to meet my needs. Any ideas, recommendations and suggestions, would love to hear. Very thank you in advance.

网友答案:

Please try this plugin, you don't need to take headache like this. you will get complete separate module for your post type with category and tag custom post type maker

1) create a post type "jobs" as per your requirement. 2) then create 2 taxonomy with name like 'Job category' & 'Job tags'. 3) when your creating the taxonomy Please select your job post type which is given at the bottom of taxonomy creation page.

finally you will get the complete separate module with following

网友答案:

Customize this code .

add_action('init','create_custom_posts'); 

function create_custom_posts(){
  register_post_type( 'cms_chooser', array(
            'labels' => array(
                'name' => 'CMS Chooser',
                'singular_name' => 'CMS Chooser',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New CMS',
                'edit_item' => 'Edit',
                'edit' => 'CMS',
                'new_item' => 'New CMS',
                'view_item' => 'View Detail',
                'search_items' => 'Search in CMS',
                'not_found' => 'No CMS found',
                'not_found_in_trash' => 'No Posts found in Trash',
                'view' => 'View Post'
            ),
            'public' => true,
            //'capability_type' => 'post',
            'hierarchical' => true,
            'exclude_from_search' => false,
            'show_in_nav_menus' =>  false,
            'query_var' => true,
            'menu_position' => 5,
            'supports' => array('title','page-attributes'),
            'has_archive' => true,
            'taxonomies' => array('cms-category'),
            'rewrite' => array('slug' => 'cms', 'with_front' => false )
        )
    );

    register_taxonomy(
        'cms-category',
        'cms_chooser',
        array(
            'labels' => array(
                'name' => 'Category',
                'singular_name' => 'Category',
                'search_items' =>  'Search Category',
                'popular_items' => 'Popular Category',
                'all_items' => 'All Category',
                'parent_item' => 'Parent Category',
                'parent_item_colon' => 'Parent Category',
                'edit_item' => 'Edit Category',
                'update_item' => 'Update Category',
                'add_new_item' => 'Add New Category',
                'new_item_name' => 'New Category Name'
            ),
            'hierarchical' => false,
            'show_in_nav_menus' =>  false,
            'exclude_from_search' => true,
            'public' => false,
            'show_ui' => true,
            'query_var' => 'cms-category',
            'show_tagcloud' => true,
            'rewrite' => true
        )
    );

}

I would have approached it this way.

  1. Create categories "latest","active","xyz","blah-blah", for this custom-post.
  2. On admin side add js to select the required two categories. (Let me know if you need code for this.)
分享给朋友:
您可能感兴趣的文章:
随机阅读: