WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Include categories for custom post type with your theme (4 posts)

  1. caputodesignz
    Member
    Posted 1 year ago #

    I have my theme setup with custom post types and I want certain categories to be installed when the theme installed. Is this possible? For example I want a "Featured" category installed with my featured-category taxonomy on theme installation. I have tried from function.php but the few things I tried didn't work. Any help would be great!

  2. linux4me2
    Member
    Posted 1 year ago #

    I think you can use wp_insert_category in your theme's functions.php to create a category. You can see the Codex article on the function here.

    I believe that if the category already exists, it isn't created judging by what it says about the function in the simpler wp_create_category function, but you should definitely test it to be sure.

    If you want to insert a taxonomy with it, too, you'll need the insert and not the create function.

  3. caputodesignz
    Member
    Posted 1 year ago #

    Thanks linux4me2, this has me in the right direction, but from what I am reading that is going to create a standard category. I need to create one for a custom post type. Here is what I have so far:

    //This is custom post type
    	add_action( 'init', 'create_post_type' );
    	function create_post_type() {
    		register_post_type( 'jobs_post',
    			array(
    				'labels' => array(
    					'name' => __( 'Jobs' ),
    					'singular_name' => __( 'Job' ),
    					'search_items' =>  __( 'Search Jobs' ),
    				    'all_items' => __( 'All Jobs' ),
    				    'parent_item' => __( 'Parent Jobs' ),
    				    'parent_item_colon' => __( 'Parent Job:' ),
    				    'edit_item' => __( 'Edit Job' ),
    				    'update_item' => __( 'Update Job' ),
    				    'add_new_item' => __( 'Add New Job' ),
    				    'new_item_name' => __( 'New Job' ),
    				),
    
    			'supports' => array( 'title'),
    			'public' => true,
    			'has_archive' => true,
    			'menu_position' => 5,
    			'rewrite' => array('slug' => 'jobs'),
    			)
    		);
    	}
    	//End
    
    	add_action( 'init', 'create_jobs2_taxonomies' );
    			function create_jobs2_taxonomies() {
    				register_taxonomy (
    					'job-type',
    					'jobs_post',
    			array(
    				'hierarchical' => true,
    				'label' => 'Job Type',
    				'show_ui' =>false,
    				'public' => true,
    				'show_admin_column' => true,
    				));
    			}

    I wanted to create default categories for the job-type category group. I thought maybe showing the code would help explain what I need. So basically I want to have categories: full-time, part-time, etc in there to start when the theme is installed.

  4. caputodesignz
    Member
    Posted 1 year ago #

    Ok I got it! I had to use wp_insert_term. Here is the updated code where I added it:

    add_action( 'init', 'create_jobs3_taxonomies' );
    			function create_jobs3_taxonomies() {
    				register_taxonomy (
    					'featured-job',
    					'jobs_post',
    			array(
    				'hierarchical' => true,
    				'label' => 'Featured',
    				'show_ui' =>true,
    				'public' => true,
    				'show_admin_column' => true,
    				));
    			wp_insert_term( 'Featured', 'featured-job', array( 'description'=> 'Featured', 'slug' => 'featured' ) );
    			}

Topic Closed

This topic has been closed to new replies.

About this Topic