Show parent category AND child category in url

Hello,

Currently I have the following structure:

-Parent Category A
–Children Category A1
–Children Category A2
-Parent Category B
–Children Category B1
–Children Category B2

If a listing belongs to the category ‘Children Category A1’ the url is this:

domain.com/children-category-a1/listing-x.

However, I need the structure to be as follows:

domain.com/parent-category-a/children-category-a1/listing-x

How can I achieve this?

Hi,

Unfortunately, there’s no such feature, it would require a custom implementation. If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work https://fvrr.co/32e7LvY

Thanks andrii.

But, removing the parent category in the url is a feature of HivePress right? I mean, by default, WordPress writes urls like this: “https://web.com/parent-cat/child-cat/page

Wouldn’t it be enough to disable the HP function that rewrites this?

Hi,

This is a non-standard WordPress permalink structure (at least for custom post types), but it may be possible with some tweaks, please try these instructions How to add custom taxonomy in custom post type permalink? | WordPress.org Listings have the “hp_listing” post type, listing categories have “hp_listing_category” taxonomy.

Thanks @andrii

I have written a function that, in my case, works well. I leave it here in case it is of help to someone else:

function custom_taxonomy_permalink($permalink, $term, $taxonomy) {
    if ($taxonomy == 'hp_listing_category' && $term->parent > 0) {
        $parent = get_term($term->parent, $taxonomy);
        $permalink = str_replace($term->slug, $parent->slug . '/' . $term->slug, $permalink);
    }
    return $permalink;
}
add_filter('term_link', 'custom_taxonomy_permalink', 10, 3);

function custom_rewrite_rule() {
    add_rewrite_rule('^listing-category/([^/]*)/([^/]*)/?', 'index.php?taxonomy=hp_listing_category&term=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rule');

Regards

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.