I have the following HTML / PHP
<h4 class="termNames">
<?php
$terms = get_terms('case_study_categories', array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array(),
) );
$exclude = array("Cleanrooms", "Mezzanine Floors", "Interiors", "Fitout");
$new_the_category = '';
foreach ($terms as $term) {
if (!in_array($term->name, $exclude)) {
?>
<li class="<?php echo $term->slug; ?>" data-filter=".<?php echo $term->slug; ?>"><span class="tick"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check" viewBox="0 0 16 16">
<path d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"/>
</svg></span> <?php echo $term->name; ?></li>
<?php } }
?>
</h4>
What I want to do is add and active class to the li that matches that term based on taxonomy url. Therefore:
If the url ends with /all-case-studies then I want to find the li with the class .all-case-studies and add a further class of active and trigger('click');
Manually the following works:
$('.term-58').each(function() {
$(this).find('.all-case-studies').addClass('active').trigger('click');
});
However you can see if I have multiple term archive pages I will have to manually keep adding the jquery depending on the body class of the term.
How can I make it dynamic where depending on the term page it will find the right class and add the extra class and trigger?
Therefore if the archive page ends /all-case-studies/leisure Then find:
<li class="<?php echo $term->slug; ?>
and add class of active and trigger