• Hello,

    My aim is to have the following URL:
    <SITE_DOMAIN>/<taxonomy_term>/<link_parameter> for a specific custom_post type.
    for example:
    <SITE_DOMAIN>/french_cars/renault

    <SITE_DOMAIN>/french_cars leads me to taxonomy-french_cars.php
    AND
    <SITE_DOMAIN>/french_cars/renault should lead to the same page with $wp_query->query_vars[‘brand’] set to ‘renault’.

    I have created a specific taxonomy for a custom post type using register_taxonomy() with the following ‘rewrite’ parameter :

    'rewrite' => array('slug' => '/', 'with_front' => false)

    (I want the taxonomy name to be omitted from the URL and only the term to show.)

    How do I set up the rewrite rule so that <SITE>/taxonomy_term/<brand_parameter> leads me to taxonomy-<term_name>.php with the <brand_parameter> set ?

    Thanks,

    Y.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    First of all, for a particular template to be used for a specific taxonomy term, the template filename should be structured like "taxonomy-{$taxonomy_slug}-{$term_slug}.php". Templates used are determined through a different process than rewrite rules. Rewrite rules determine the query vars used, which only indirectly relate to templates used.

    While it’s possible to not have a base permalink term like the taxonomy name in your permlinks, it can cause other problems. When you do that, every permalink generated needs to include a term for that taxonomy. Without a fixed base term, WP cannot tell the difference between taxonomy terms and other slugs that also occur in the first permalink position. Let’s say you have a page titled Renault and a child of that page titled Espace. The permalink for such a page would be example.com/renault/espace/. Without a base term for your taxonomy, the rewrite rule you end up with will take that request and query for a post called Espace which is assigned the taxonomy term Renault. That might return a valid post, but it’s not the page that it was supposed to get. More than likely, the query will come back with no posts found even though there is a valid page with that permalink.

    But if your site would not involve such ambiguity, use add_rewrite_rule(). The rule would assign anything in the first position as a taxonomy term and anything in the second position as the brand query var. Your regexp would be similar to the last example with the base term “nutrition/” removed. And of course assigning to taxonomy and brand query vars instead of page_id and food.

    Assuming there is a template for the taxonomy term, it’ll be used regardless of the brand specified. If there is no matching term template, unless you have a generic taxonomy template, archive.php is probably the template that will be used. It depends on the templates available in your theme.

Viewing 1 replies (of 1 total)

The topic ‘rewrite rule for specific taxnomy’ is closed to new replies.