Sunday, February 26, 2023

How to create custom post type with taxonomy in wordpress




function portfolio_custom_post_type() {
	$labels = array(
		'name'                => __( 'Portfolio' ),
		'singular_name'       => __( 'Portfolio'),
		'menu_name'           => __( 'Portfolio'),
		'parent_item_colon'   => __( 'Parent Portfolio'),
		'all_items'           => __( 'All Portfolio'),
		'view_item'           => __( 'View Portfolio'),
		'add_new_item'        => __( 'Add New Portfolio'),
		'add_new'             => __( 'Add New'),
		'edit_item'           => __( 'Edit Portfolio'),
		'update_item'         => __( 'Update Portfolio'),
		'search_items'        => __( 'Search Portfolio'),
		'not_found'           => __( 'Not Found'),
		'not_found_in_trash'  => __( 'Not found in Trash')
	);
	$args = array(
		'label'               => __( 'Portfolio'),
		'description'         => __( 'Portfolio'),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor',
 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
		'public'              => true,
		'hierarchical'        => false,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
        'menu_icon'           => 'dashicons-portfolio',
		'show_in_admin_bar'   => true,
		'has_archive'         => true,
		'can_export'          => true,
		'exclude_from_search' => false,
	    'yarpp_support'       => true,
		'taxonomies' 	      => array('portfolio-category,post_tag'),
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
        //'rewrite' => array('slug' => 'portfolio'),
);
	register_post_type( 'portfolio', $args );
}
add_action( 'init', 'portfolio_custom_post_type', 0 );

// Let us create Taxonomy for Custom Post Type
add_action( 'init', 'portfolio_custom_taxonomy', 0 );
 
//create a custom taxonomy name it "type" for your posts
function portfolio_custom_taxonomy() {
 
    $labels = array(
        'name' => _x( 'Portfolio category', 'taxonomy general name' ),
        'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Portfolio' ),
        'all_items' => __( 'All Portfolio' ),
        'parent_item' => __( 'Parent Portfolio' ),
        'parent_item_colon' => __( 'Parent Portfolio:' ),
        'edit_item' => __( 'Edit Portfolio' ), 
        'update_item' => __( 'Update Portfolio' ),
        'add_new_item' => __( 'Add New Portfolio' ),
        'new_item_name' => __( 'New Portfolio Name' ),
        'menu_name' => __( 'Portfolio Category' ),
    ); 	

    register_taxonomy('portfolio-category',array('portfolio'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        //'rewrite' => array( 'slug' => 'projects-category' ),
    ));
}

1 comment:

1 hour Free Service for WordPress website :)

      1 hour Free Service for WordPress website :)