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'),
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 created a custom post type with name services and added taxonomy with name catservice. I named my taxonomy file taxonomy-catservice.php and its not working, I also tried it by renaming it to taxonomy-services but it also didn't work. What should I do?
It is hard to tell without see the code from you posttype and you custom taxonomy.
But the best tip I can give you ehre is to check you custom_post code where you create it.
and see so you have included that you custom post type has included you taxonomy.
For example see this code:
$args = array(
'label' => __( 'CUstom post type', 'domain' ),
'description' => __( 'description', 'domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
'taxonomies' => array( 'custom_taxonomy'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'custom_post_type', $args );
And the important thing there is to check that the key taxonomies is there and it you custom taxnonomy name is there.
That or you can do it from your custom taxonomy when you register it like this:
register_taxonomy( 'custom_taxonomy', array( 'custom_post_type_name' ), $args );
you see the second param in that function gets an array with you custom post type name in it.
Hope this might give you some help :)
EDIT:
After I did run a test with that plugin, so when I create my Custom Taxonomy after I created the Custom post type, I could choose what post types that custom taxonomy should be linked to. See image below:
And that worked for me, so that taxonomy is now displayed add avaible to add to my custom post type
Try with this code, this is working on my end, Also update the permalink after adding the code in function.php.
$labels = array(
'name' => _x( 'Services', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'Service', 'Post type singular name', 'textdomain' ),
'menu_name' => _x( 'Services', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Service', 'Add New on Toolbar', 'textdomain' ),
'add_new' => __( 'Add New', 'textdomain' ),
'add_new_item' => __( 'Add New Service', 'textdomain' ),
'new_item' => __( 'New Service', 'textdomain' ),
'edit_item' => __( 'Edit Service', 'textdomain' ),
'view_item' => __( 'View Service', 'textdomain' ),
'all_items' => __( 'All Services', 'textdomain' ),
'search_items' => __( 'Search Services', 'textdomain' ),
'parent_item_colon' => __( 'Parent Services:', 'textdomain' ),
'not_found' => __( 'No services found.', 'textdomain' ),
'not_found_in_trash' => __( 'No services found in Trash.', 'textdomain' ),
'featured_image' => _x( 'Service Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'archives' => _x( 'Service archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
'insert_into_item' => _x( 'Insert into services', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
'uploaded_to_this_item' => _x( 'Uploaded to this services', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
'filter_items_list' => _x( 'Filter services list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
'items_list_navigation' => _x( 'Services list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
'items_list' => _x( 'Services list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
);
$args = array(
'label' => __( 'Services', 'domain' ),
'description' => __( 'description', 'domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
'taxonomies' => array( 'catservice'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'services', $args );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Service Categories', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Service Category', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Service Categories', 'textdomain' ),
'popular_items' => __( 'Popular Service Categories', 'textdomain' ),
'all_items' => __( 'All Service Categories', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Service Category', 'textdomain' ),
'update_item' => __( 'Update Service Category', 'textdomain' ),
'add_new_item' => __( 'Add New Service Category', 'textdomain' ),
'new_item_name' => __( 'New Service Category Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate service categories with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove service categories', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used service categories', 'textdomain' ),
'not_found' => __( 'No service categories found.', 'textdomain' ),
'menu_name' => __( 'Service Categories', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'catservice' ),
);
register_taxonomy( 'catservice', array( 'services' ), $args );
Note: Update permalink from the settings options.
I have created a new custom taxonomy in WordPress functions.php file but when I want to add new post or edit one I cannot found my custom taxonomy meta box in the right side of the admin menu
my functions.php code for custom taxonomy:
function create_business_taxonomy() {
$labels = array(
'name' => _x( 'Businesses', 'taxonomy general name' ),
'singular_name' => _x( 'Business', 'taxonomy singular name' ),
'search_items' => __( 'Search Businesses' ),
'all_items' => __( 'All Businesses' ),
'parent_item' => __( 'Parent Business' ),
'parent_item_colon' => __( 'Parent Business:' ),
'edit_item' => __( 'Edit Business' ),
'update_item' => __( 'Update Business' ),
'add_new_item' => __( 'Add New Business' ),
'new_item_name' => __( 'New Business Name' ),
'menu_name' => __( 'Business' ),
);
register_taxonomy('business-type',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array( 'business-type' => 'topic' ),
));
}
add_action( 'init', 'create_business_taxonomy', 0 );
but I can't find in where I marked in the screenshot:
Did I forget or missed something?
You need to set 'show_in_rest' => true. ( (boolean) (optional) Whether to include the taxonomy in the REST API. You will need to set this to true in order to use the taxonomy in your gutenberg metablock. ). So your code will be like this:
function create_business_taxonomy() {
$labels = array(
'name' => _x( 'Businesses', 'taxonomy general name' ),
'singular_name' => _x( 'Business', 'taxonomy singular name' ),
'search_items' => __( 'Search Businesses' ),
'all_items' => __( 'All Businesses' ),
'parent_item' => __( 'Parent Business' ),
'parent_item_colon' => __( 'Parent Business:' ),
'edit_item' => __( 'Edit Business' ),
'update_item' => __( 'Update Business' ),
'add_new_item' => __( 'Add New Business' ),
'new_item_name' => __( 'New Business Name' ),
'menu_name' => __( 'Business' ),
);
register_taxonomy('business-type',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array( 'business-type' => 'topic' ),
));
}
add_action( 'init', 'create_business_taxonomy', 0 );
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/
See here is no tags box how can I add a tag box in the right panel please help me.
Tags are just a taxonomy type so you can create a new taxonomy for your page post type:
add_action( 'init', 'create_tags_for_pages_taxonomy', 500, 0);
function create_tags_for_pages_taxonomy() {
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => __( 'Parent Tag' ),
'parent_item_colon' => __( 'Parent Tag:' ),
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'menu_name' => __( 'Tags' ),
);
// Now register the taxonomy
register_taxonomy('page_tags',array('release'), array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}