Skip Navigation

[Resolved] Show parents subcategories while in a subcategory

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Marcel 5 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1312411
subcat.jpg

I've created a category archive for my custom post type. I've created a view to show the subcategories of the current parent category. I've used the Taxonomy parent filter "Select taxonomy terms whose parent is the current taxonomy archive" to achieve that, which works fine.

However, we want those subcategories also on a subcategorie page. Now, if I select a subcategory, the subcategory menu disappears. We want to keep it, and style the current subcategory like I can with a normal nav menu. Is that possible?

#1312489

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thanks for waiting.

When on a child term archive the taxonomy filter that sets the parent according to the current taxonomy archive uses the child term, but you need it to be its parent.

The only solution that comes to mind is to modify the query arguments for your View that outputs the terms which you insert into the archive.

If the current archive term has no parent then we don't need to change anything, but if it does have a parent, that's what we need to use.

The following code should achieve that, if you want to test it and confirm that it works. You'll need to edit the ID of the View.

function ts_mod_tax_archive( $query_settings, $view_settings, $view_id  ){

    if ( $view_id == '202' ){

        global $wp_query;
        $obj = $wp_query->get_queried_object();

        if ( $obj->parent != 0 ){

            $query_settings['child_of'] = $obj->parent;
        }
    }
    return $query_settings;
}
add_filter( 'wpv_filter_taxonomy_query', 'ts_mod_tax_archive', 101, 3 );
#1312517

Hi Nigel,

Thanks, this works!

Now I just need to add a class to the current subcategorie so I can style it. Something like the "current-menu-item" class in WP nav menu...

#1312857

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You have a View that iterates over and outputs the terms.

So in the output section of the View you can wrap the term titles in spans and conditionally include a current-term class.

You'll use the "conditional output" button to insert a wpv-conditional shortcode, testing the result of the wpv-taxonomy-title or wpv-taxonomy-slug shortcodes (probably safer to use the slug: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-taxonomy-slug) against the slug of the current archive term.

Except! We don't have a way to output the slug of the current archive term.

I've shared a custom shortcode previously which you can use for this: hidden link

You'll need to register that taxonomy shortcode at Toolset > Settings > Front-end Content

You can output the slug of the current archive term to use on the right hand side of your condition like so: [taxonomy output='slug']

Do you want to try that and let me know how you get on?

#1313015

This was quite advanced for me, I expected this to be easier with Toolset to be honest since it's a pretty common thing. But I was able to get it to work with your help, thanks for that!