I have created a custom posts with the following code:
// Our custom post type function
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tours'),
'hierarchical' => true
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'rewrite' => array('slug' => 'tours')
);
//register_taxonomy( 'tour_category', 'tours', $args );
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
In my permalink settings I have setup:
http://mydomainname.com.au/%category%/%postname%/
I want to display the URLs like so:
display posts in categories
http://mydomainname.com.au/tours/gbr/
http://mydomainname.com.au/tours/whitehaven-tours/
http://mydomainname.com.au/tours/day-tours/
Display posts
http://mydomainname.com.au/tours/gbr/name-of-post
http://mydomainname.com.au/tours/whitehaven-tours/name-of-post
http://mydomainname.com.au/tours/day-tours/name-of-post
But I'm currently getting the following URLs:
display posts in categories
http://mydomainname.com.au/tour_category/gbr/
http://mydomainname.com.au/tour_category/whitehaven-tours/
http://mydomainname.com.au/tour_category/day-tours/
Display posts
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
I am not familiar enough with URL rewrites to make the necessary changes. I have read many articles but getting nowhere fast. Any help in the right direction would be greatly appreciated.
Related
I am trying to get posts related to custom taxonomy.
I have a new CPT called article.
I have two custom taxonomies related to the CPT:tags and cats
I can see the cpt items in a custom archive page called archive-article.php.
the problem is this, I created two new files: taxonomy-cats.php and 'taxonomy-tags.php' the content of this files are exacley as the archive-article.php. file.
I get a massage that nothing was found. I checked and I know that there are custom posts related to this taxonomy value.
This is the url I receive the empty archive:
'http://localhost/broker/cats/sales/'
What can be the problem?
Thank you!
This is the taxonomy code:
function create_cats_taxonomy() {
$labels = array(
'name' => _x( 'cats', 'taxonomy general name' ),
'singular_name' => _x( 'cat', 'taxonomy singular name' ),
'search_items' => __( 'search items' ),
'all_items' => __( 'all items' ),
'parent_item' => __( 'parent item' ),
'parent_item_colon' => __( 'parent item colon:' ),
'edit_item' => __( 'edit item' ),
'update_item' => __( 'update item' ),
'add_new_item' => __( 'add new item' ),
'new_item_name' => __( 'new cat' ),
'menu_name' => __( 'cat'),
);
// Now register the taxonomy
register_taxonomy('cats',array('article'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'cats' ),
));
}
add_action( 'init', 'create_cats_taxonomy' );
I'm trying to separate the category lists for a specific post type "announcement" from the post categories.
I want my announcement post type to have its own category list and not mix with the post category list
Here's my code
function announcement_post_types() {
register_post_type(
'announcement',
array(
'labels' => array(
'name' => __( 'Announcements' ),
'singular_name' => __( 'announcement' ),
'menu_name' => __( 'Announcements' ),
'all_items' => __( 'All Announcements' ),
'view_item' => __( 'View Announcement' ),
'add_new_item' => __( 'New Announcement' ),
'add_new' => __( 'New Announcement' ),
'edit_item' => __( 'Edit Announcements' ),
'update_item' => __( 'Update' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'Not Found' ),
'not_found_in_trash' => __( 'Not Found in Trash' ),
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'announcement'),
'menu_icon' => 'dashicons-admin-page',
'supports' => array('title', 'editor', 'custom-fields')
)
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'rewrite' => true
)
);
register_taxonomy_for_object_type('category', 'announcement');
}
add_action('init', 'announcement_post_types');
If this code is working, this is what links the regular categories to the announcement post type:
register_taxonomy_for_object_type('category', 'announcement');
We need to exchange it with the new taxonomy you created and registered, so exchange it with this:
register_taxonomy_for_object_type('announcement_type', 'announcement');
Let me know if this did the trick for you, there might be more issues here, but this might also be enough.
UPDATE:
I removed the register_taxonomy_for_object_type('category', 'announcement'); and changed into this
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Announcement Category' ),
'update_item' => __( 'Update Announcement Category' ),
'add_new_item' => __( 'Add New Announcement Category' ),
'new_item_name' => __( 'New Announcement Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove Announcement categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'label' => __('Announcement Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'rewrite' => true
)
);
It works for me now,
If you have better answer for this please share :) I want to make this code shorter.
How to create a second category menu like Seminars >> seminars >> create new seminars >> seminar categories and then I want to duplicate seminar categories and make it Seminar venues with inside remaining the same.
See attached screenshot:
As per my understanding, you just want to create a new taxonomy under Seminars. If you make changes in theme files you can edit the function.php and add the following code
$labels = array(
'name' => _x( 'Seminar Venue', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Venue', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Venue', 'textdomain' ),
'all_items' => __( 'All Venues', 'textdomain' ),
'parent_item' => __( 'Parent Venue', 'textdomain' ),
'parent_item_colon' => __( 'Parent Venue:', 'textdomain' ),
'edit_item' => __( 'Edit Venue', 'textdomain' ),
'update_item' => __( 'Update Venue', 'textdomain' ),
'add_new_item' => __( 'Add New Venue', 'textdomain' ),
'new_item_name' => __( 'New VenueName', 'textdomain' ),
'menu_name' => __( 'Venue', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'seminars' ), $args );
Or you can use the plugin Custom Post Type UI. In this plugin go to taxonomy and add a new taxonomy under your post type "seminars"
I'm feeling close to finishing this. In short, I can't seem to display posts.
For example I have:
http://somedomain.org/tours //Displays all categories
http://somedomain.org/tours/gbr //Displays all posts in category
http://somedomain.org/tours/gbr/my-post //Should display post in category
http://somedomain.org/tours/gbr/my-post doesn't display, I get a there's nothing here, 404 error. But if I go to:
http://somedomain.org/toursgbr/my-post
It does in fact, display the post ("toursgbr" has no forward slash if you're looking for the difference)
I am using the latest WordPress. I am using the plugin:
Custom Post Type Permalinks
I have the following code in the functions.php file:
// Our custom post type function
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tours', 'with_front' => TRUE),
'cptp_permalink_structure' => '%tour_category%/%postname%',
'hierarchical' => true //A added
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'rewrite' => array('slug' => 'tours')
);
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
The line of code:
'cptp_permalink_structure' => '%tour_category%/%postname%',
If I add the forward slash, so it's:
'cptp_permalink_structure' => '/%tour_category%/%postname%',
That's when I can't view posts. Any help in the right direction would be fantastic. I do have other categories other than gbr and posts can have multiple categories. I thought that might be important information
I created a custom taxonomy called instrument_categories. It works fine but I want the link structure to be like this:
www.example.com/instruments/term/subterm
Instead, I get this:
www.example.com/instruments/term/
www.example.com/instruments/subterm
I've read a dozen posts about this, and I can't find anything wrong with my code:
add_action( 'init', 'create_instrument_taxonomies', 0 );
function create_instrument_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Instrument Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Instrument Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Instrument Categories' ),
'popular_items' => __( 'Popular Instrument Categories' ),
'all_items' => __( 'All Instrument Categories' ),
'parent_item' => __( 'Parent Instrument Category' ),
'parent_item_colon' => __( 'Parent Instrument Category:' ),
'edit_item' => __( 'Edit Instrument Category' ),
'update_item' => __( 'Update Instrument Category' ),
'add_new_item' => __( 'Add New Instrument Category' ),
'new_item_name' => __( 'New Instrument Category Name' ),
);
register_taxonomy('instrument_categories',array('sdp_instrument'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'instruments', 'hierarchal' => true, 'with_front' => true ),
));
}
Am I missing something? I've made sure to refresh the permalinks. Why won't this work?
You're really doing it wrong.
Everything you need to know is here : http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/