Community Forums › Forums › Archived Forums › General Discussion › Custom Taxonomy URL
- This topic has 9 replies, 2 voices, and was last updated 9 years, 9 months ago by
Porter.
-
AuthorPosts
-
April 4, 2015 at 1:42 pm #146682
Porter
ParticipantI have a structure like so:
Dining - (Custom Post Type)
dining_types (Custom Taxonomy belonging to Dining Custom Post Type)
-restaurants (term)
-cafes (term)
-delis (term)
-street-vendors (term)I'm trying to programmatically assign custom templates for the following:
-Custom Post Type (Dining) site.com/dining
-Terms of dining_types custom taxonomy (Restaurants) site.com/dining/restaurants (desired url)I've got the conditional check to assign the template (here's a piece of it)
function anib_taxonomy_templates( $original_template ) { if (is_post_type('dining')) { return get_stylesheet_directory_uri() . '/custom-templates/template-test.php'; } else { return $original_template; } } add_filter( 'template_include', 'anib_taxonomy_templates' );
I know have to use is_tax for the terms. That being said, I can't even find the archive page for the terms. I thought it would be site.com/dining/term-name, or site.com/dining/custom-taxonomy-name/term-name, but it's not at either. I have no idea what url it's at, and more importantly, I need to MAKE it the way I want it using the rewrite API (I assume). To the best of my knowledge, my structure perfectly matches that of the default category / tag structure, and I should be able to get this working. I will have other custom post types and taxonomies, but if I can get one working, I should be able to get them all.
I would love a helping hand in sorting this out, as I'm at a loss and 20 tabs open isn't leading me to what I need.
April 4, 2015 at 3:30 pm #146688Porter
ParticipantSo I found the url the term is currently at - site.com/dining_types/restaurants
That being said, I still need to figure out how to get the structure site.com/dining/restaurants, aka site.com/custom-post-type/term-name (leaving out the custom taxonomy, and using just the CPT and term). Would setting the "slug" field on the custom taxonomy to "dining" work? I just noticed this (making an edit), but I'm unsure if having the slug of the custom taxonomy the same as the custom post type will have any conflicts.
April 5, 2015 at 3:37 pm #146760Porter
ParticipantA quick update to this:
I can get is_post_type_archive to work (site.com/dining), but for whatever reason, I can't seem to get the taxonomy to work (site.com/dining/restaurants). The page is an archive, but my conditional check isn't working. I used:
if (is_tax('dining', 'restaurants')) {
I've tried just doing dining, to see if ALL terms would display in such a way, but no luck. What conditional check do I need to get site.com/dining/restaurants to use a custom archive? Again, dining is the custom post type, restaurants is a "term" of the "dining_types (slug "dining") custom taxonomy.
April 5, 2015 at 5:16 pm #146765Porter
ParticipantI wish I could edit a post later, rather than within the first 15 minutes of posting. Anyway:
I changed the rewrite slug of my custom taxonomy to "dining_types" instead of dining, and my conditional works now. This means that my custom post of "dining" is somehow conflicting with the rewrite slug of "dining" for my custom taxonomy.
Between with_front, hierarchical, and the rest of the information I've taken in, I'm completely swamped / stumped. I feel like this should be relatively easy to achieve, but I just can't get it. If anyone could lend me a hand in putting this multi-week nightmare to rest, I'd be extremely grateful.
April 6, 2015 at 1:13 am #146778Brad Dalton
ParticipantDon't you want to change the slug for register_taxonomy not the URL?
You can change it in.
'rewrite' => array( 'slug' => 'portfolio-type', 'with_front' => false ),
And then re-save your Permalinks.
You'll also need to use the WordPress Template Hierarchy to name your files correctly so they're loaded correctly.
Or
You can use template_include with the correct conditional tag to force an archive to use a specific custom template.
I think your problem is with the incorrect naming of your files for each term.
April 6, 2015 at 10:32 am #146816Porter
ParticipantI am changing the slug in the rewrite (which changes the url), and I am re-saving my permalinks each time. I am NOT creating any files for the templates, as I don't want them, I'm only using template_include to assign templates to these files. Here is my register taxonomy code:
function anib_custom_taxonomies() { // Add new "Types" taxonomy to Posts register_taxonomy('dining_types', 'dining', array( // Hierarchical taxonomy (like categories) 'hierarchical' => true, // This array of options controls the labels displayed in the WordPress Admin UI 'labels' => array( 'name' => _x( 'Types', 'taxonomy general name' ), 'singular_name' => _x( 'Location', 'taxonomy singular name' ), 'search_items' => __( 'Search Types' ), 'all_items' => __( 'All Types' ), 'parent_item' => __( 'Parent Location' ), 'parent_item_colon' => __( 'Parent Location:' ), 'edit_item' => __( 'Edit Location' ), 'update_item' => __( 'Update Location' ), 'add_new_item' => __( 'Add New Location' ), 'new_item_name' => __( 'New Location Name' ), 'menu_name' => __( 'Types' ), ), // Control the slugs used for this taxonomy 'rewrite' => array( 'slug' => 'dining', // This controls the base slug that will display before each term 'with_front' => false, // Don't display the category base before "/Types/" 'hierarchical' => false// This will allow URL's like "/Types/boston/cambridge/" ), )); }
The issue is (from my testing), that when the rewrite slug is "dining" (what I want the url to be, site.com/dining/restaurants, restaurants being a term of dining_types), it breaks. I believe this is because the Custom Post Type the taxonomy belongs to is ALSO named dining, so the slug of dining is being used by both, and somehow conflicting. I changed the taxonomy slug to dining_types in the rewrite, and the conditional for template_include worked, but will NOT work when it's "dining". That being said, the slug rewrite is 100% useless to me if I can't get it to be "dining", as that's what I want the url to be.
April 6, 2015 at 10:49 am #146820Porter
ParticipantIt's worth noting that my permalink structure is still /%category%/%postname%, as I don't know what it needs to be changed to work with custom post types and taxonomies, if anything. I think I may need to add some code to my site-specific plugin using the rewrite API, but I'm not sure.
April 6, 2015 at 11:34 pm #146881Porter
ParticipantAfter a lot of testing, I confirmed I had a permalink conflict.
"A common issue that arises when you create your own permalinks are permalink conflicts. Permalink conflicts occur when two permalinks share the same regular expression structure."
My custom post type slug of "dining" was conflicting with the rewrite slug of "dining" for the custom taxonomy. I thought I was safe in giving them unique names, but apparently the names mean very little, and it's the slug that causes issues (or perhaps both).
What I've determined, is that I either need to disable the custom post type archive, or disable taxonomy archives (but not terms). This is because I want:
site.com/dining (custom taxonomy archive OR custom post type archive, but NOT both)
site.com/dining/cafes (term archive)Am on the right track? Any ideas how to go about that?
April 9, 2015 at 8:34 pm #147265Porter
ParticipantI'm still desperately seeking help with this.
Seeing as I can't have the same slug for the custom post type, and the custom taxonomy, I need a way to work around that to get my url structure. All I need is this:
site.com/dining (works)
site.com/dining/specific-location (404)
site.com/dining/custom-term-a (works)
site.com/dining/custom-term-b (works)I refuse to believe this isn't possible, it's so basic, and so logical. Let me add that I also don't need WordPress to do anything with these by default, I'm adding my own templates, so "messing up default behavior" isn't an issue, I just need those urls to work. There must be a rewrite rule, or something out there, that will allow this.
May 10, 2015 at 9:43 am #151495Porter
Participant -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.