I have created a custom taxonomy for WooCommerce products.
function add_hunts_type(){
// Car Hunts
$cHunt_labels = array(
'name' => __( 'Hunts', 'pixar' ),
'singular_name' => __( 'Hunt', 'pixar' ),
'search_items' => __( 'Search Hunts', 'pixar' ),
'all_items' => __( 'All Hunts', 'pixar' ),
'parent_item' => __( 'Parent Hunt', 'pixar' ),
'parent_item_colon' => __( 'Parent Hunt:', 'pixar' ),
'edit_item' => __( 'Edit Hunt', 'pixar' ),
'update_item' => __( 'Update Hunt', 'pixar' ),
'add_new_item' => __( 'Add New Hunt', 'pixar' ),
'new_item_name' => __( 'New Hunt', 'pixar' ),
'menu_name' => __( 'Hunts', 'pixar' ),
);
$cHunt_args = array(
'labels' => $cHunt_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'carhunt' )
);
register_taxonomy( 'car_Hunt', 'product', $cHunt_args );
}
add_action( 'init', 'add_hunts_type', 0 );
/* Flush rewrite rules for custom post types. */
add_action( 'after_switch_theme', 'flush_rewrite_rules' );
This is working when I login into admin. When I want to try from the front end it's not working.
I have change permalinks
I have deleted .htaccess and regenerated it
I have other inbuilt things which are working properly
Please let me know if I've missed something.
Register Custom Taxonomy in WooCommerce
add below function in function.php
<?php
// Register Custom Taxonomy
function custom_taxonomy_Hunt() {
$labels = array(
'name' => 'Hunts',
'singular_name' => 'Hunt',
'menu_name' => 'Hunt',
'all_items' => 'All Hunts',
'parent_item' => 'Parent Hunt',
'parent_item_colon' => 'Parent Hunt:',
'new_item_name' => 'New Hunt Name',
'add_new_item' => 'Add New Hunt',
'edit_item' => 'Edit Hunt',
'update_item' => 'Update Hunt',
'separate_items_with_commas' => 'Separate Hunt with commas',
'search_items' => 'Search Hunts',
'add_or_remove_items' => 'Add or remove Hunts',
'choose_from_most_used' => 'Choose from the most used Hunts',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'carhunt' )
);
register_taxonomy( 'carhunt', 'product', $args );
register_taxonomy_for_object_type( 'carhunt', 'product' );
}
add_action( 'init', 'custom_taxonomy_Hunt' );
?>
Related
I want to register my own taxonomy in standard, WP posts but it doesn't work. Look at my code
register_taxonomy(
'special-category',
array('post', 'books'),
array(
'hierarchical' => false,
'labels' => array(
//...
),
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'cpecial-category' )
)
);
Tin my post type books it displays correctly but in standard wp post not. Is it possible to register own taxonomy in standard posts?
Try registering taxonomy in init hook.
Try out this code.
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
function create_subjects_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Search Subjects' ),
'all_items' => __( 'All Subjects' ),
'parent_item' => __( 'Parent Subject' ),
'parent_item_colon' => __( 'Parent Subject:' ),
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Add New Subject' ),
'new_item_name' => __( 'New Subject Name' ),
'menu_name' => __( 'Subjects' ),
);
// Now register the taxonomy
register_taxonomy(
'special-category',
array('post', 'books'),
array(
'hierarchical' => false,
'labels' => array(
//..
),
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'cpecial-category' )
)
);
}
I have a taxonomy which I have created as follows:
function understrap_custom_taxonomies() {
$post_types_index = array('cs_publications', 'cs_projects');
$taxs = array(
'cs_themes' => array(
'menu_title' => 'Themes',
'plural' => 'Themes',
'singular' => 'Theme',
'hierarchical' => true,
'slug' => 'theme',
'post_type' => array( 'post', 'cs_publications', 'cs_projects', 'cs_events')
)
);
foreach( $taxs as $tax => $args ) {
$labels = array(
'name' => _x( $args['plural'], 'taxonomy general name' ),
'singular_name' => _x( $args['singular'], 'taxonomy singular name' ),
'search_items' => __( 'Search '.$args['plural'] ),
'all_items' => __( 'All '.$args['plural'] ),
'parent_item' => __( 'Parent '.$args['plural'] ),
'parent_item_colon' => __( 'Parent '.$args['singular'].':' ),
'edit_item' => __( 'Edit '.$args['singular'] ),
'update_item' => __( 'Update '.$args['singular'] ),
'add_new_item' => __( 'Add New '.$args['singular'] ),
'new_item_name' => __( 'New '.$args['singular'].' Name' ),
'menu_name' => __( $args['menu_title'] )
);
$tax_args = array(
'hierarchical' => $args['hierarchical'],
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'with_front' => false,
'rewrite' => array( 'slug' => $args['slug'] ),
);
register_taxonomy( $tax, $args['post_type'], $tax_args );
}
}
add_action('init', 'understrap_custom_taxonomies');
I am now trying to get the value for "cs_themes" to display in a WP Bakery grid using the custom field element:
However no matter whether I type "cs_themes", "taxonomy_cs_themes" or any other logical combination, it doesn't seem to pull through the values. Any advice would be much appreciated.
I have a custom post type called Resources:
register_post_type(
'resources',
build_post_args(
'resources', 'Resource', 'Resources',
array(
'menu_icon' => 'dashicons-welcome-write-blog',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array('editor', 'title','author','thumbnail', 'revisions'),
'taxonomies' => array('sector', 'subject', 'type'),
)
)
);
I have also created the taxonomy of type which is assigned to Resources:
register_taxonomy(
'type',
'type',
array(
'hierarchical' => true,
'label' => 'Type',
'query_var' => true,
'rewrite' => array(
'slug' => 'type',
'with_front' => false
)
)
);
In the WordPress backend, here are all my type's:
However, when I go to Appearance > Menus > and click on the Categories dropdown, only these options show:
Why is this?
Hello Please add below code and check as you are missing lots of things when creating resources post type. (Note: Please remove all your code added for creating resources post and type taxonomy)
function ro_resources_custom_post_type() {
$labels = array(
'name' => __( 'Resources' ),
'singular_name' => __( 'Resource'),
'menu_name' => __( 'Resources'),
'parent_item_colon' => __( 'Parent Resource'),
'all_items' => __( 'All Resources'),
'view_item' => __( 'View Resource'),
'add_new_item' => __( 'Add New Resource'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Resource'),
'update_item' => __( 'Update Resource'),
'search_items' => __( 'Search Resource'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash')
);
$args = array(
'label' => __( 'resources'),
'description' => __( 'Best Resources'),
'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,
'show_in_admin_bar' => true,
'has_archive' => true,
'can_export' => true,
'exclude_from_search' => false,
'yarpp_support' => true,
'publicly_queryable' => true,
'capability_type' => 'page'
);
register_post_type( 'resources', $args );
}
add_action( 'init', 'ro_resources_custom_post_type', 0 );
add_action( 'init', 'ro_create_resources_custom_taxonomy', 0 );
//create a custom taxonomy name it "type" for your posts
function ro_create_resources_custom_taxonomy() {
$labels = array(
'name' => _x( 'Types', 'taxonomy general name' ),
'singular_name' => _x( 'Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'all_items' => __( 'All Types' ),
'parent_item' => __( 'Parent Type' ),
'parent_item_colon' => __( 'Parent Type:' ),
'edit_item' => __( 'Edit Type' ),
'update_item' => __( 'Update Type' ),
'add_new_item' => __( 'Add New Type' ),
'new_item_name' => __( 'New Type Name' ),
'menu_name' => __( 'Types' ),
);
register_taxonomy('types',array('resources'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
));
}
Tested and works well
I created custom post type "testimonials" with custom taxomies
it works well except of sinbgle page, when I click on permalink it shows "not found".
Here is my code for custom post types/taxonomies:
function testimonials_custom_post_type()
{
$labels = array(
'name' => _x('Testimonials', 'Post type general name'),
'singular_name' => _x('Testimonial', 'Post type singular name'),
'add_new' => _x('Add new Testimonial', 'Grower'),
'add_new_item' => __('Add new Testimonial'),
'edit_item' => __('Edit Testimonial'),
'new_item' => __('New Testimonial'),
'all_items' => __('All Testimonials'),
'view_item' => __('View Testimonial'),
'search_items' => __('Search Testimonials'),
'not_found' => __('No testimonials found'),
'not_found_in_trash' => __('No testimonials found in Trash'),
'parent_item_colon' => '',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => false,
'query_var' => false,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'exclude_from_search' => false,
'supports' => array('title', 'editor', 'thumbnail' ),
'menu_position' => 6,
'menu_icon' => 'dashicons-id',
'taxonomies' => array( 'subjects' ),
);
register_post_type('testimonials', $args);
}
add_action('init', 'testimonials_custom_post_type');
/*register custom taxonomies for testimonials*/
add_action( 'init', 'create_testimonials_taxonomies', 0 );
function create_testimonials_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Subjects', 'subjects', 'textdomain' ),
'singular_name' => _x( 'Subject', 'subject', 'textdomain' ),
'search_items' => __( 'Search Subject', 'textdomain' ),
'all_items' => __( 'All Subjects', 'textdomain' ),
'edit_item' => __( 'Edit Subject', 'textdomain' ),
'update_item' => __( 'Update Subject', 'textdomain' ),
'add_new_item' => __( 'Add new Subject', 'textdomain' ),
'new_item_name' => __( 'New Category Subject', 'textdomain' ),
'menu_name' => __( 'Subject', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'subjects' ),
);
register_taxonomy( 'subjects', array( 'testimonials' ), $args );
}
/*register custom taxonomies for testimonials*/
I tried to use flush_rewrite_rules( false ); and tried to update permalinks in wp admin panel it's still not working
For custom post type "testimonials" I created single-testimonials.php in my child theme
Thank you very much for any help!
To fix my bug I had to set 'publicly_queryable' to 'true'
and it works now ))
You could try to create taxonomy-subjects.php and refresh your permalinks after you try each time.
So I'm using a custom theme and would like to create a custom url structure for our articles..
Basically the one I have on right now is -> site.com/article/article-slug
And i needed to have a permalink like this site.com/category-slug/article/article-slug
The code i'm using is :
register_taxonomy( 'article', 'article_type', array(
'labels' => array(
'name' => 'Article Categories' ,
'singular_name' => _x( 'Article Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Article Categorys' ),
'popular_items' => __( 'Popular Article Categorys' ),
'all_items' => __( 'All Article Categorys' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Article Category' ),
'update_item' => __( 'Update Article Category' ),
'add_new_item' => __( 'Add Article Category' ),
'new_item_name' => __( 'New Article Category Name' ),
'separate_items_with_commas' => __( 'Separate Article Categorys with commas' ),
'add_or_remove_items' => __( 'Add or remove Article Categorys' ),
'choose_from_most_used' => __( 'Choose from the most used Article Categorys' )
) ,
'hierarchical' => true,
'query_var' => true,
'show_ui' => true,
'rewrite' => array('slug' => 'article-category') ) );
register_post_type( 'article_type',
array(
'labels' => array('name' => 'Article Manager', 'singular_name' => 'Articles' ),
'query_var' => true,
'show_ui' => true,
'public' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'article','with_front' => TRUE),
'supports' => array ( 'title', 'editor','author', 'custom-fields', 'revisions', 'post-formats', 'trackbacks', 'comments','excerpt' ),
'menu_icon' => get_template_directory_uri()."/PPT/img/admin/article.png",
)
);
What about Settings > Permalinks and /%category%/%taxonomy%/postname%/?