I have CPT called forms_cpt and a Taxonomy called forms_tax. In my page-forms.php I am able to crate a list of forms_tax terms like
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
which is creating a list of terms. but whenever I click on them it landed in a wrong URL. For example if click on soccer term it landed at
http://www.domain.ca/soccer/
instead of
http://www.domain.ca/Forms/soccer/
What am I doing wrong?
function gen_forms_tax() {
$labels = array(
'name' => 'Form Types',
'singular_name' => 'Form Type',
'menu_name' => 'Form Types',
'all_items' => 'All Form Types',
'parent_item' => 'Form Parent',
'parent_item_colon' => 'Form Parents:',
'new_item_name' => 'New Form Name',
'add_new_item' => 'Add New Form',
'edit_item' => 'Edit Form',
'update_item' => 'Update Form',
'separate_items_with_commas' => 'Separate Form with commas',
'search_items' => 'Search Form',
'add_or_remove_items' => 'Add or remove Form',
'choose_from_most_used' => 'Choose from the most used Form',
'not_found' => 'Form Not Found',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'forms_tax', array( 'forms_cpt' ), $args );
}
add_action( 'init', 'gen_forms_tax', 0 );
function gen_forms_cpt() {
$labels = array(
'name' => 'Forms',
'singular_name' => 'Form',
'menu_name' => 'Form',
'name_admin_bar' => 'Form',
'parent_item_colon' => 'Form Parent:',
'all_items' => 'All Forms',
'view_item' => 'View Forms',
'add_new_item' => 'Add New Form',
'add_new' => 'Add New Form',
'new_item' => 'New Form',
'edit_item' => 'Edit Form',
'update_item' => 'Update Form',
'search_items' => 'Search Form',
'not_found' => 'Form Not found',
'not_found_in_trash' => 'Form Not found in Trash',
);
$args = array(
'description' => 'This Post Type Adds Form to Website',
'labels' => $labels,
'supports' => array( 'title', 'excerpt', ),
'taxonomies' => array( 'forms_tax' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'rewrite' => array( 'slug' => 'Forms','with_front' => true, 'hierarchical' => true ),
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'forms_cpt', $args );
}
add_action( 'init', 'gen_forms_cpt', 0 );
Solution : You need to follow the steps given below.
1) Go to Settings from Admin panel.
2) Go to permalinks
3) Save the permalinks
It should work after you save the parmalinks. You always need update the permalinks once you add any rewrite rule to the site.
Related
Hi I've created a custom post type named as stores. Stores includes two custom taxonomies,
States :
enter image description here
Cities :
enter image description here
Now I want to show states in cities taxonomy so when user adds a city it should fall under some state.
Reason behind to do this I've created a filter page so whenever user selects state in dropdown it should return cities associated with that state in other dropdown so that my queries can work perfectly
Here's how filter are :
enter image description here
Right now it's showing cities from all states
Here's the code for registering Custom Post type :
function create_store_function(){
$labels = array(
'name' => _x('Stores', 'post type general name', 'your_text_domain'),
'singular_name' => _x('Store', 'post type Singular name', 'your_text_domain'),
'add_new' => _x('Add Store', '', 'your_text_domain'),
'add_new_item' => __('Add New Store', 'your_text_domain'),
'edit_item' => __('Edit Store', 'your_text_domain'),
'new_item' => __('New Store', 'your_text_domain'),
'all_items' => __('All Stores', 'your_text_domain'),
'view_item' => __('View Stores', 'your_text_domain'),
'search_items' => __('Search Store', 'your_text_domain'),
'not_found' => __('No Store found', 'your_text_domain'),
'not_found_in_trash' => __('No Store on trash', 'your_text_domain'),
'parent_item_colon' => '',
'menu_name' => __('Stores', 'your_text_domain'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'store'),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'taxonomies' => array('State'),
'menu_icon' => 'dashicons-format-gallery',
'supports' => array('title', 'editor', 'excerpt', 'thumbnail')
);
$states = array(
'name' => __('State'),
'singular_name' => __('State'),
'search_items' => __('Search'),
'popular_items' => __('More Used'),
'all_items' => __('All States'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Add new'),
'update_item' => __('Update'),
'add_new_item' => __('Add new State'),
'new_item_name' => __('New'),
'show_admin_column' => true
);
$cities = array(
'name' => __('City'),
'singular_name' => __('City'),
'search_items' => __('Search'),
'popular_items' => __('More Used'),
'all_items' => __('All Cities'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Add new'),
'update_item' => __('Update'),
'add_new_item' => __('Add new City'),
'new_item_name' => __('New'),
'show_admin_column' => true
);
register_taxonomy('store_state', array('store'), array(
'labels' => $states,
'singular_label' => 'store_state',
'all_items' => 'State',
'rewrite' => array('slug' => 'state'),
'hierarchical' => True,
'public' => true,
'publicly_queryable' => true,
'show_in_quick_edit' => true,
'show_admin_column' => true,
)
);
register_taxonomy('store_cities', array('store'), array(
'labels' => $cities,
'singular_label' => 'store_cities',
'all_items' => 'Cities',
'rewrite' => array('slug' => 'city'),
'hierarchical' => True,
'public' => true,
'publicly_queryable' => true,
'show_in_quick_edit' => true,
'show_admin_column' => true,)
);
register_post_type('store', $args);
flush_rewrite_rules();
}
add_action('init', 'create_store_function');
And searchbar code :
<form action="" method="get" autocomplete="off" id="search-filter">
State
'store_state', 'orderby' => 'name' ) ) ) :
echo 'Select category...';
foreach ( $states as $state ) :
$count = $state->count;
echo 'name . '">' . $state->name . " ($count) " . '';
endforeach;
echo '';
endif;
?>
City
'store_cities', 'orderby' => 'name' ) ) ) :
echo 'Select category...';
foreach ( $cities as $city ) :
$count2 = $city->count;
echo 'name . '">' . $city->name . " ($count2) " . '';
endforeach;
echo '';
endif;
?>
Am I missing something or can it be achieved without creating any relationship between taxonomies and using logics in code?
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.
I have created custom post type "directory" and registered custom taxonomy "directory-category". But unable to access taxonomy page "taxonomy-directory-category.php". Getting "Error 404 - Not Found" error.
function create_post_type_listing() {
register_post_type('directory',
array(
'labels' => array(
'name' => __( 'Listings'),
'singular_name' => __( 'Listing'),
'add_new' => __('Add New Listing' ),
'add_new_item' => __('Add New Listing'),
'edit' => __( 'Edit Listing' ),
'edit_item' => __( 'Edit Listing' ),
'new_item' => __( 'New listing' ),
'view' => __( 'View Listing' ),
'view_item' => __( 'View Listing' ),
'search_items' => __( 'Search Listings' ),
'not_found' => __( 'No listings found' ),
'not_found_in_trash' => __( 'No listings found in Trash' ),
'featured_image' => __( 'Listing Image' ),
'set_featured_image' => __( 'Set Listing Image' ),
'remove_featured_image' => __( 'Remove Listing Image' ),
'use_featured_image' => __( 'Use Listing Image' )
),
'public' => true,
'menu_position' => 5,
'menu_icon' => plugins_url( 'images/listing-20x20.png', __FILE__ ),
'rewrite' => array(
'slug' => __('directory')
),
'supports' => array( 'title','editor','thumbnail')));
}
add_action( 'init', 'create_listing_taxonomies', 0 );
function create_listing_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => 'Listing Categories',
'singular_name' => 'Listing Category',
'search_items' => 'Listing Categories',
'all_items' => 'All Listing Categories',
'parent_item' => 'Parent Listing Category',
'parent_item_colon' => 'Parent Listing Category:',
'edit_item' => 'Edit Listing Category',
'update_item' => 'Update Listing Category',
'add_new_item' => 'Add New Listing Category',
'new_item_name' => 'New Listing Category',
'menu_name' => 'Listing Category',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'directory-category' ),
);
register_taxonomy( 'directory-category', array( 'directory' ), $args );
}
function listing_flush_rules() {
//defines the post type so the rules can be flushed.
create_post_type_listing();
//and flush the rules.
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'listing_flush_rules' );
add_action( 'init', 'create_post_type_listing' );
Kindly help me with taxonomy page structure.
Regards,
Shubhajeet Saha
Please add below code into your functions.php or inside your plugin page
add_action('init', 'create_taxonomies', 0);
function create_taxonomies() {
//change start///
$title = 'Your title';
$slug = 'yourslug';
$your_plugin_textdomain = 'textdomain';
//change end///
$labels = array(
'name' => _x($title, $your_plugin_textdomain),
'singular_name' => _x($title, $your_plugin_textdomain),
'search_items' => __('Search Genres'),
'all_items' => __('All ' . $title),
'parent_item' => __('Parent ' . $title),
'parent_item_colon' => __('Parent ' . $title . ':'),
'edit_item' => __('Edit ' . $title),
'update_item' => __('Update ' . $title),
'add_new_item' => __('Add New ' . $title),
'new_item_name' => __('New ' . $title . ' Name'),
'menu_name' => __( ucfirst($title)),
);
$args = array(
'hierarchical' => true,
'labels' => ($labels),
'show_ui' => true,
// 'show_admin_column' => true, // to display taxonomy in post table
'query_var' => true,
'rewrite' => array('slug' => $slug),
);
register_taxonomy($slug, array($catname[$key]), $args);
}
i made custom taxonomy for woocomerce products, products will be books and ecery book have his own writter.
<pre>function custom_taxonomy_writter() {
$labels = array(
'name' => 'Writters',
'singular_name' => 'Writter',
'menu_name' => 'Writter',
'all_items' => 'All writters',
'parent_item' => 'Parent writter',
'parent_item_colon' => 'Parent writter:',
'new_item_name' => 'New writter name',
'add_new_item' => 'Add new writter',
'edit_item' => 'Edit writter',
'update_item' => 'Update writter',
'separate_items_with_commas' => 'Separate writter with commas',
'search_items' => 'Search writters',
'add_or_remove_items' => 'Add or remove writters',
'choose_from_most_used' => 'Choose from the most used writters',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy('writter', 'product', $args);
}
add_action('init', 'custom_taxonomy_writter', 0);</pre>
Screenshoot from wp admin:
Now I need to display it for example under the product title, and needs to bi link, like tags or categories.
Wooola!
<?php echo get_the_term_list( $post->ID, 'writter', '', ', ' ); ?>
Simple and works!!! And is also linked to author with listing of his books :)