I have made a new plugin for my WordPress site, which has the capability of creating a new post type called products and on this plug in I have added a custom taxonomy called product types. Everything is fine but my Wordpress admin panel custom post type 'product' still showing the categories option, which I don't want. That's my plug in code,
<?php function FLChildTheme_custom_post_type (){
$labels = array(
'name' => 'Products',
'singular_name' => 'products',
'add_new' => 'Add Products',
'all_items' => 'All items',
'add_new_item' => 'Add item',
'edit_item' => 'Edit Item',
'new_item' => 'New Item',
'view_item' => 'View Products',
'search_item' => 'Search Product',
'not_found' => 'No Products Found',
'not_found_in_trash' => 'No product in trash',
'parent_item_colon' => 'Parent Item',
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'revisions',
),
'taxonomies' => array('category','post_tag'),
'menu_position' => 5,
'exclude_from_search' => false,
);
register_post_type('products', $args);
}
add_action ('init', 'FLChildTheme_custom_post_type');
/* Custom post type End*/
/* Add custom taxonomies */
function FLChildTheme_add_custom_taxonomies() {
// Add new "Locations" taxonomy to Posts
register_taxonomy('Producttypes', 'products', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Product Types', 'taxonomy general name' ),
'singular_name' => _x( 'Product Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Product Types' ),
'all_items' => __( 'All Product Types' ),
'parent_item' => __( 'Parent Product Types' ),
'parent_item_colon' => __( 'Parent Product Types:' ),
'edit_item' => __( 'Edit Product Types' ),
'update_item' => __( 'Update Product Types' ),
'add_new_item' => __( 'Add New Product Types' ),
'new_item_name' => __( 'New Product Type Name' ),
'menu_name' => __( 'Product Types' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'Producttypes', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'FLChildTheme_add_custom_taxonomies', 0 );
?>
Related
I have created a Custom post type called 'event' and I'm adding a custom taxonomy for this post type. I have created 3 tags in the custom taxonomy but when I am editing a new event post, the taxonomies are not returned and it shows "no tags found".
below is the code for the cpt and taxonomy. Am I missing something.
-- create the custom post type
function bps_event_posttype() {
$labels = array(
'name' => _x( 'Events', 'post type general name' ),
'singular_name' => _x( 'Event', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Event' ),
'add_new_item' => __( 'Event' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'all_items' => __( 'All Events' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search Events' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Events');
$args = array(
'labels' => $labels,
'description' => 'custom event',
'public' => true,
'exclude_from_search' => false,
'query_var' => true,
'show_in_menu' => true,
'menu_position' => 4,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
'publicly_queryable' => true,
'supports' => array( 'title', 'editor', 'thumbnail',
'excerpt','page-attributes'),
'taxonomies' => array('eventtype','location'),
'hierarchical' => false,
'has_archive' => true
);
register_post_type( 'event', $args );
}
add_action( 'init', 'bps_event_posttype' );
-- create the taxonomy and assign it to the event cpt
function add_event_type_taxonomy() {
register_taxonomy('eventtype', array('event'), array(
'public' => false,
'show_admin_column' => true,
'show_ui' => true,
'query_var' => true,
'labels' => array(
'name' => _x( 'Event Types', 'taxonomy general name' ),
'singular_name' => _x( 'Event Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Event Types' ),
'all_items' => __( 'All Event Types' ),
'parent_item' => __( 'Parent Event Type' ),
'parent_item_colon' => __( 'Parent Event Type:' ),
'edit_item' => __( 'Edit Event Type' ),
'update_item' => __( 'Update Event Type' ),
'add_new_item' => __( 'Add New Event Type' ),
'new_item_name' => __( 'New Event Type Name' ),
'menu_name' => __( 'Event Types' ),
),
'rewrite' => array(
'slug' => 'types',
'with_front' => false,
'hierarchical' => false
),
));
}
add_action( 'init', 'add_event_type_taxonomy', 0 );
register_taxonomy_for_object_type('eventtype', 'event');
When editing a event post, if I select "Choose from the most used tags", it displays "No tags found." but there are 3 tags in the taxonomy edit screen.
I have added your code in my localhost and checked that everything is okay in your coding part, there is nothing wrong, but what you have missed it that you need to select the tags at least one single event to see it in the "choose from the most used tags" area. For the very first time it will not show any tags there. Please see the screenshot https://prnt.sc/UujJXicZ729G . It is perfectly showing the tags. Hope you will be able to select tags from the chosen area after selecting a tags once.
I would like to create a unique design category for my custom post type. Here I get it design for main post type but the same design not applying for the category pages it takes to the default category page.
My Custom Post Type name resources for that I create a separate designed template archive-resources.php, so it's working fine but for custom post category page I tried in many ways but still I won't get the result (created separate Taxonomy like category-resources.php) but still not works.
Once I added a category for my custom post type resources as hotels for the hotels category I get default post category design but I won't what that. I like the same design want custom post type resources.
This is my custom post type functions.php
function custom_resource_type() {
$labels = array(
'name' => _x( 'Resources', 'Post Type General Name', 'gucherry-blog' ),
'singular_name' => _x( 'Resource', 'Post Type Singular Name', 'gucherry-blog' ),
'menu_name' => __( 'Resources', 'gucherry-blog' ),
'parent_item_colon' => __( 'Parent Resource', 'gucherry-blog' ),
'all_items' => __( 'All Resources', 'gucherry-blog' ),
'view_item' => __( 'View Resource', 'gucherry-blog' ),
'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ),
'add_new' => __( 'Add Resource', 'gucherry-blog' ),
'edit_item' => __( 'Edit Resource', 'gucherry-blog' ),
'update_item' => __( 'Update Resource', 'gucherry-blog' ),
'search_items' => __( 'Search Resource', 'gucherry-blog' ),
'not_found' => __( 'Not Found', 'gucherry-blog' ),
'not_found_in_trash' => __( 'Not found in Trash', 'gucherry-blog' ),
);
$args = array(
'label' => __( 'resources', 'gucherry-blog' ),
'description' => __( 'Resource for software products and online', 'gucherry-blog' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( '' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'menu_icon' => 'dashicons-admin-site-alt2',
'capability_type' => 'page',
);
register_post_type( 'resources', $args );
}
add_action( 'init', 'custom_resource_type', 0 );
function resource_categories_taxonomy() {
$resource_cats = array(
'name' => _x( 'Resource Categories', 'taxonomy general name', 'gucherry-blog' ),
'singular_name' => _x( 'Resource Category', 'taxonomy singular name', 'gucherry-blog' ),
'search_items' => __( 'Search Resource Categories', 'gucherry-blog' ),
'all_items' => __( 'All Resource', 'gucherry-blog' ),
'parent_item' => __( 'Parent Resource', 'gucherry-blog' ),
'parent_item_colon' => __( 'Parent Resource:', 'gucherry-blog' ),
'edit_item' => __( 'Edit Resource', 'gucherry-blog' ),
'update_item' => __( 'Update Resource', 'gucherry-blog' ),
'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ),
'new_item_name' => __( 'New Resource', 'gucherry-blog' ),
'menu_name' => __( 'Resource Categories', 'gucherry-blog' ),
);
register_taxonomy('resource_categories',array('resources'), array(
'hierarchical' => true,
'labels' => $resource_cats,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resource' ),
));
}
add_action( 'init', 'resource_categories_taxonomy', 0 );
You want to edit the design of your taxnonomy, so the way the template structure looks like in wordpress, you should name your file:
taxonomy-resource_categories.php
The taxonomy.php is used if you want to edit the archive page of an taxonomy term, if you add the name of the taxonomy, it will only be used for a specific taxonomy and not all taxonomies. So we had to add the name of your custom taxonomy after.
This is a very useful link for your problem, hope it helps you out:
https://developer.wordpress.org/themes/basics/template-hierarchy/
I'm developing a Wordpress site where I need to use pages that show a group of portfolio items of the same category of a certain custom post type. I can't make this work because I'm using CPTUI (Custom Post Type UI plugin).
So now I tried it with a clean install and without CPTUI plugin, but with some code I found, still no luck.
This is the CPT code I'm using:
function portfolio_register() {
$labels = array(
'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio Items'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => 8,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'portfolio' , $args );
}
add_action('init', 'portfolio_register');
This is the registration of the taxonomy I'm using:
function create_portfolio_taxonomies() {
$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 Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
);
register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args );
}
add_action( 'init', 'create_portfolio_taxonomies', 0 );
So I have created a portfolio item and a category called: websites.
Now I want to show all the portfolio items that have this category websites.
And I want to use the url: mywebsite.com/portfolio/categories/websites
How can this be done, why am I getting a 404 page error?
i don't know how to create a new custom post type in WordPress so can you please show me how to create it.
In below function you can create new custom post type in WordPress.
function my_first_custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Movie', 'twentythirteen' ),
'update_item' => __( 'Update Movie', 'twentythirteen' ),
'search_items' => __( 'Search Movie', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'my_first_custom_post_type', 0 );
I hope this answer is useful for you to create a new custom post type in WordPress.
I added some custom post types and custom taxonomies in my function.php and they work great. I assigned some hierarchical and non-hierarchical taxonomies (catagories & tags right?) to the custom post types. The non-hierarchical taxonomies work great and I can add new items in the tags metaboxes when editing the post. However, this does not work for my hierarchical taxonomies! I have no idea where the error lies, but the buttons "add new vehicle category" and "most used" do not work add all, you can click them but nothing loads. Is this something with javascript?
here's an screenshot to show the metaboxes I'm talking about:
Here's one of my registered custom post types:
$labels = array(
'name' => _x( 'Finished Projects', 'post type general name' ),
'singular_name' => _x( 'Finished Project', 'post type singular name' ),
'menu_name' => _x( 'Finished Projects', 'admin menu' ),
'name_admin_bar' => _x( 'Finished Project', 'add new on admin bar' ),
'add_new' => _x( 'Add New', 'Finished Project' ),
'add_new_item' => __( 'Add New Finished Project' ),
'new_item' => __( 'New Finished Project' ),
'edit_item' => __( 'Edit Finished Project' ),
'view_item' => __( 'View Finished Project' ),
'all_items' => __( 'All Finished Projects' ),
'search_items' => __( 'Search Finished Projects' ),
'parent_item_colon' => __( 'Parent Finished Projects:' ),
'not_found' => __( 'No Finished Projects found.' ),
'not_found_in_trash' => __( 'No Finished Projects found in Trash.' )
);
$args = array(
'labels' => $labels,
'description' => 'Page template for Finished Projects',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'finished_projects' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('excerpt','comments','author'),
'taxonomies' => array('scales', 'manufacturers','product categories','vehicle categories','countries'),
);
register_post_type( 'finishedprojects', $args );
and here's one of my registered custom taxonomies:
//Vehicle Categories
$labels = array(
'name' => _x( 'Vehicles Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Vehicle Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Vehicles Categories' ),
'all_items' => __( 'All Vehicles Categories' ),
'parent_item' => __( 'Parent Vehicle Category' ),
'parent_item_colon' => __( 'Parent Vehicle Category:' ),
'edit_item' => __( 'Edit Vehicle Category' ),
'update_item' => __( 'Update Vehicle Category' ),
'add_new_item' => __( 'Add New Vehicle Category' ),
'new_item_name' => __( 'New Vehicle Category Name' ),
'menu_name' => __( 'Vehicle Categories' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'vehicle_categories' ),
);
register_taxonomy( 'vehicle category', array( 'reviews','finishedprojects' ), $args );
Do you guys need more info or is this enough information?
Thanks!
Remove the space from the name of the taxonomy.
To do this, replace:
register_taxonomy( 'vehicle category', array( 'reviews','finishedprojects' ), $args );
with:
register_taxonomy( 'vehicle_category', array( 'reviews','finishedprojects' ), $args );
Also, replace:
'taxonomies' => array('scales', 'manufacturers','product categories','vehicle categories','countries'),
with:
'taxonomies' => array('scales', 'manufacturers','product_categories','vehicle_categories','countries'),