Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Check if is a search page before add active class to menu item #223

Merged
merged 2 commits into from
Dec 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions modules/nav-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class NavWalker extends \Walker_Nav_Menu {
private $archive; // Stores the archive page for current URL

public function __construct() {
$cpt = get_post_type();
$this->cpt = in_array($cpt, get_post_types(array('_builtin' => false)));
$this->archive = get_post_type_archive_link($cpt);
$cpt = get_post_type();
$this->cpt = in_array($cpt, get_post_types(array('_builtin' => false)));
$this->archive = get_post_type_archive_link($cpt);
$this->is_search = is_search();
}

public function checkCurrent($classes) {
Expand All @@ -46,7 +47,7 @@ public function display_element($element, &$children_elements, $max_depth, $dept

$element->is_active = (!empty($element->url) && strpos($this->archive, $element->url));

if ($element->is_active) {
if ($element->is_active && !$this->is_search) {
$element->classes[] = 'active';
}

Expand All @@ -61,7 +62,7 @@ public function cssClasses($classes, $item) {
if ($this->cpt) {
$classes = str_replace('current_page_parent', '', $classes);

if ($this->archive) {
if ($this->archive && !$this->is_search) {
if (Utils\url_compare($this->archive, $item->url)) {
$classes[] = 'active';
}
Expand Down