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
Related
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.
My custom post type was working fine on wordpress 4.9 so i updated to 5.2 and it is not working now.
This is the page which contains the testimonials slider at bottom clicking on read more link redirects back to home page:
https://openlibrary.ecampusontario.ca/
I have changed Permalinks settings many time but no Luck.
class testimonial {
function __construct() {
add_action('init',array($this,'create_post_type'));
add_action('init',array($this,'create_taxonomies'));
}
function create_post_type() {
$labels = array(
'name' => 'Testimonials',
'singular_name' => 'Testimonial',
'menu_name' => 'Testimonials',
'name_admin_bar' => 'Testimonial',
'add_new' => 'Add New',
'add_new_item' => 'Add New Testimonial',
'new_item' => 'New Testimonial',
'edit_item' => 'Edit Testimonial',
'view_item' => 'View Testimonial',
'all_items' => 'All Testimonials',
'search_items' => 'Search Testimonials',
'parent_item_colon' => 'Parent Testimonial',
'not_found' => 'No Testimonials Found',
'not_found_in_trash' => 'No Testimonials Found in Trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-appearance',
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'author', 'excerpt', 'custom-fields'),
'has_archive' => true,
'rewrite' => array( 'slug' => 'testimonials' ),
'query_var' => true
);
register_post_type( 'testimonial', $args );
}
function create_taxonomies() {
// Add a taxonomy like categories
$labels = array(
'name' => 'Types',
'singular_name' => 'Type',
'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',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
);
register_taxonomy('testimonial_type',array('testimonial'),$args);
// Add a taxonomy like tags
$labels = array(
'name' => 'Tags',
'singular_name' => 'Tag',
'search_items' => 'Tags',
'popular_items' => 'Popular Tags',
'all_items' => 'All Tags',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit Tag',
'update_item' => 'Update Tag',
'add_new_item' => 'Add New Tag',
'new_item_name' => 'New Tag Name',
'separate_items_with_commas' => 'Separate Tags with commas',
'add_or_remove_items' => 'Add or remove Tags',
'choose_from_most_used' => 'Choose from most used Tags',
'not_found' => 'No Tags found',
'menu_name' => 'Tags',
);
$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' => 'tag' ),
);
register_taxonomy('testimonial_tag','testimonial',$args);
}
}
new testimonial();
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.
I'm creating a custom post type with taxonomies in WordPress 4.7.3, but for some reason the taxonomy pages aren't working properly. My custom post type is "events", but the archive-events.php template file isn't working when visiting "mysite.com/events/event-category" or when visiting "mysite.com/events"
Below is my code from functions.php, what am I doing wrong here??? lol
add_action( 'init', 'register_events', 20 );
function register_events() {
$labels = array(
'name' => _x( 'All Events', 'events','sonal' ),
'singular_name' => _x( 'Event', 'events', 'sonal' ),
'add_new' => _x( 'Add New', 'events', 'sonal' ),
'add_new_item' => _x( 'Add New Event', 'events', 'sonal' ),
'edit_item' => _x( 'Edit Event', 'events', 'sonal' ),
'new_item' => _x( 'New Event', 'events', 'sonal' ),
'view_item' => _x( 'View Event', 'events', 'sonal' ),
'search_items' => _x( 'Search Events', 'events', 'sonal' ),
'not_found' => _x( 'No Events found...', 'events', 'sonal' ),
'not_found_in_trash' => _x( 'No Events found in Trash', 'events', 'sonal' ),
'parent_item_colon' => _x( 'Parent Event:', 'events', 'sonal' ),
'menu_name' => _x( 'Events', 'events', 'sonal' ),
);
$args = array(
'labels' => __( $labels, 'local' ),
'hierarchical' => true,
'description' => 'events',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'revisions' ),
'taxonomies' => array( 'events_category'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-tickets-alt',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
'public' => true,
'has_archive' => 'events_category',
'capability_type' => 'post'
);
register_post_type( 'events', $args );
}
//Create Taxonomies (Categories)
add_action( 'init', 'create_events_taxonomies', 20 );
function create_events_taxonomies() {
$labels = array(
'name' => _x( 'Event Categories', 'taxonomy general name', 'sonal' ),
'singular_name' => _x( 'Event Category', 'taxonomy singular name', 'sonal' ),
'search_items' => __( 'Search Event Categories', 'sonal' ),
'all_items' => __( 'All Event Categories', 'sonal' ),
'parent_item' => __( 'Parent Event Category', 'sonal' ),
'parent_item_colon' => __( 'Parent Event Category:', 'sonal' ),
'edit_item' => __( 'Edit Event Category', 'sonal' ),
'update_item' => __( 'Update Event Category', 'sonal' ),
'add_new_item' => __( 'Add New Event Category', 'sonal' ),
'new_item_name' => __( 'New Event Category Name', 'sonal' ),
'menu_name' => __( 'Event Categories', 'sonal' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
);
register_taxonomy( 'events_category', array( 'events' ), $args );
}
//Set Permalinks
function wpa_events_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'events' ){
$terms = wp_get_object_terms( $post->ID, 'events_category' );
if( $terms ){
return str_replace( '%events_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_events_permalinks', 1, 2 );
Remove the /%events_category% from the rewrite argument of the events custom post type
Before:
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
After:
'rewrite' => array('slug' => 'events','with_front' => FALSE),
Again in the args of the events custom post type, set "has_archive" to true
Before
'has_archive' => 'events_category',
After:
'has_archive' => true,
Refresh permalink in Setting -> permalinks
Be sure that you chose custom structure with
/%category%/%postname%/
Then go to
yoursite.com/events/
the archive-events.php file should work
My code in functions.php is:
register_post_type('final_access_sites', array(
'public' => true,
'labels' => array(
'name' => 'Post a site',
'add_new' => 'Add new site',
'add_new_item' => 'Add new site',
'edit_item' => 'Update an existing site',
'view_item' => 'View the site',
'all_items' => 'All sites',
),
'taxonomies' => array('category'),
'menu_position' => 30,
'menu_icon' => get_template_directory_uri().'/images/final_post.png',
'supports' => array('thumbnail','title'),
'order' => 'ASC',
));
After adding this code, I don't see category taxonomy in my dashboard for any post. I don't understand why? I have searched about it but didn't find any reason. Please help me to solve this issue.
Use the register_taxonomy_for_object_type function to add a registered taxonomy to a registered Post Type.
add_action('init','add_categories_to_cpt');
function add_categories_to_cpt(){
register_taxonomy_for_object_type('category', 'final_access_sites');
}
Hey maybe you can try this.
It works because it is form a actual project. It has two parts:
First: custom post type and Second: the category for it.
You just have to copy it and change the custom post type Name and the category name
have you and i hope it helps.! :)
function custom_post_type_projekt() {
$labels = array(
'name' => _x( 'Projekt', 'Post Type General Name', 'projekt' ),
'singular_name' => _x( 'Projekt', 'Post Type Singular Name', 'projekt' ),
'menu_name' => __( 'Projekte', 'projekt' ),
'parent_item_colon' => __( 'Eltern-Element:', 'projekt' ),
'all_items' => __( 'Alle Projekte', 'projekt' ),
'view_item' => __( 'Projekt ansehen', 'projekt' ),
'add_new_item' => __( 'Neues Projekt hinzufügen', 'projekt' ),
'add_new' => __( 'Neues Projekt', 'projekt' ),
'edit_item' => __( 'Projekt bearbeiten', 'projekt' ),
'update_item' => __( 'Projekt aktualisieren', 'projekt' ),
'search_items' => __( 'Projekt suchen', 'projekt' ),
'not_found' => __( 'Projekt nicht gefunden', 'projekt' ),
'not_found_in_trash' => __( 'Projekt nicht im Papierkorb gefunden', 'projekt' ),
);
$args = array(
'label' => __( 'Projekte', 'projekt' ),
'description' => __( 'Hier können beliebig viele Projekte angelegt werden', 'projekt' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'custom-fields', ),
// 'taxonomies' => array( 'category' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-media-document',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'post',
);
register_post_type( 'projekt', $args );
}
// Hook into the 'init' action
add_action( 'init', 'custom_post_type_projekt', 0 );
function my_taxonomies_product() {
$labels = array(
'name' => _x( 'Projektkategorien', 'taxonomy general name' ),
'singular_name' => _x( 'Projektkategorien', 'taxonomy singular name' ),
'search_items' => __( 'Suche Projekt Kategorien' ),
'all_items' => __( 'Alle Projektkategorien' ),
'parent_item' => __( 'Parent Projektkategorien' ),
'parent_item_colon' => __( 'Parent Projektkategorien:' ),
'edit_item' => __( 'Edit Projektkategorien' ),
'update_item' => __( 'Update Projektkategorien' ),
'add_new_item' => __( 'Neu Projektkategorie hinzufügen' ),
'new_item_name' => __( 'Neue Projekt Kategorie' ),
'menu_name' => __( 'Projektkategorien' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'projektkategorien', 'projekt', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );