CMSCSS
CMSCSS

Reputation: 2176

php main menu - add active class to parent if children is being viewed

I've read many answers (many seem to be Wordpress-specific though) but don't understand how to apply them to this situation.

I'm setting an active class on nav items like this:

<li class="<?= ($_SERVER['REQUEST_URI'] == '/about' ? 'active' : ''); ?>">

Question:

Is there a way to get php to exclude everything after /about so the active class gets applied to any child with a parent of /about/?

Something like this?

'/about/*'

Any pointers in the right direction would be much appreciated.

Upvotes: 0

Views: 101

Answers (1)

Jonathan
Jonathan

Reputation: 2877

try this

<li class="<?= (strpos($_SERVER['REQUEST_URI'],'/about') === 0 ? 'active' : ''); ?>">

Upvotes: 1

Related Questions