• Resolved ilyaol123

    (@ilyaol123)


    Hello, everyone!
    I have a hierarchy of categories:
    Catalog
    -Cars
    — Alfa Romeo
    — Audi
    — BMW
    -Trucks
    — Alfa Romeo
    — Audi
    — BMW
    The brands of cars can be in different types of vehicles.
    Here is the problem. I have the pages:
    site.ru/catalog/ — a catalog page.
    site.ru/catalog/cars — a page with car brands.
    site.ru/catalog/trucks/bmw — a product page of a particular brand and type of car.
    site.ru/catalog/cars/bmw/product — a product page.
    I want to avoid duplicating terms, create 2 different taxonomies, and add new rewrite rules for the catalog page.
    But then what to do with broken links like site.ru/catalog/cars/blablabla or site.ru/catalog/blablabla/bmw, which should redirect to the 404-page?
    Maybe it’s better to parse query variables, check information about the URL exists in my database, and if it does not do a redirect to 404-page?
    Maybe do you know a more elegant solution?

    • This topic was modified 5 years, 6 months ago by ilyaol123.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Your description doesn’t say quite enough to understand what you have and what you want to have.
    If catalog is a Page, then anything with catalog in the URL would be a child Page of it.
    URLs for taxonomy terms will show posts with that term. The URL with the taxonomy root will give a 404, but it looks like a Page URL, so just make a Page for it. (I wrote a theme that supplies a taxonomy root Page template, and it shows the content of the Page followed by a list of term links.)

    You do not need to parse query variables. That’s what WordPress does already.

    I understand what you would like to do, you need to create different taxanomy inside the file function.php (REMEMBER MAKE TWO BACKUP OF THIS FILE FIRST):

    I’m modifying code from the Italian version of Smashing WordPress of Thord Daniel Hedengren, use this cose inside a fresh test installation of wordpress then try inside your website

    add_action( 'init', 'cars', 0 );
    function cars(){
     register_taxonomy ('alfa romeo', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Alfa Romeo',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    register_taxonomy ('audi', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Audi',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    register_taxonomy ('bmw', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'BMW',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    }
    
    function trucks(){
     register_taxonomy ('alfa romeo', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Alfa Romeo',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    register_taxonomy ('audi', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Audi',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    register_taxonomy ('bmw', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'BMW',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    }
    

    For more info : https://codex.www.ads-software.com/Function_Reference/register_taxonomy
    or
    https://wpshout.com/create-wordpress-custom-taxonomy/
    but i think you can copy paste that code on YOUR TEST WEBSITE!. (REMEMBER MAKE 2 BACKUP OF THIS FILE FIRST)

    For the 404 and redirection try this plugin https://www.ads-software.com/plugins/redirection/

    Let me know if these suggestions works ??

    Thread Starter ilyaol123

    (@ilyaol123)

    Thank you, guys, for your replies!
    But you don’t understand what I want to do.
    I’d like to create only 2 different taxonomies: car and truck (for the first time). And create a hierarchical structure in every brand. For example:
    Catalog
    -Cars
    — Alfa Romeo
    — Alfa Romeo Giulia
    —- …
    —- …
    — Alfa Romeo Brera
    — Audi
    — BMW
    -Trucks
    — Alfa Romeo
    — Alfa Romeo 1
    — Alfa Romeo 2
    —- …
    —- …
    — Audi
    — BMW

    
    add_action( 'init', 'register_taxonomies', 0 );
    function register_taxonomies(){
     register_taxonomy ('car', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Cars',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    register_taxonomy ('truck', 'post',
    array (
           'hierarchical'=>false,
           'label'=>'Trucks',
           'query_var'=>true,
           'rewrite'=>true
           )
    );
    }
    

    Alfa Romeo, Audi, BMW and other brands are Categories.
    But I don’t know, how to create such structure without duplicating brands (they will relate to different taxonomies and it means I have to duplicate brands because “track Audi” and “car Audi” are different. In the beginning, I wanted to use pages, but it is not possible to set multiple parent pages for one page. And now I want to use categories, but I can’t get rid of duplication because I have some brands which are different types of vehicles.

    I’ll try on a test site this evening or max tomorrow evening, meanwhile wait for other help ??

    You are trying to mix different things.
    If it’s a noun, like “Car” or “Truck”, that’s a post or post type. Each one has a specific name as in the detail of one vehicle, and that includes its Make and Model and Year (perhaps). These are properties of the noun.
    Taxonomies are ways to organize the nouns. You can use categories, which are usually all one kind of thing, so that each post is in only one category. Or you can use a flat list like tags, which can be any kind of thing and put as many as you want on each post. (you can query for matching multiple tags at a time pretty easily — example.com/tag/blue+sunroof)

    So, try not to mix organization that belongs in a hierarchical taxonomy with descriptors that belong in a flat taxonomy, and don’t use nouns where adjectives go.
    You can have lots of different taxonomies, but the more you have, the more difficult it is to combine them in one query. There are plugins for that, but keep it simple.

    I helped organize this site: https://www.moriareviews.com/ for movies.
    It has several taxonomies: category (used for genre), ratings, directors, theme, actors, year. I had some links for multiple things like the example above, but the owner removed those from the menu. You can see it’s very similar to your vehicle problem.

    Thread Starter ilyaol123

    (@ilyaol123)

    Thank you, guys!
    I got the point. I should reorganize my site’s structure.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to create friendly URL for different taxonomies?’ is closed to new replies.