S
S
SpectreZ2020-05-20 17:18:37
WordPress
SpectreZ, 2020-05-20 17:18:37

Why menu item in Wordpress is not marked as active?

Good afternoon.

In the main menu of a wordpress site, menu items leading to the post archive are not marked as active when viewing a single post page. How can this be set up?
The link to the entry page looks like domain.com/services/service-1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2020-05-20
@wppanda5

Look at the source code of the menu, see the class in it at the active menu item, for example, current_page_item , cover the lazy ass of the theme creator who did not write styles for it. You write them down yourself.
For example so. Profit!!!
.current_page_item > a{ color: red}

S
SpecterZ, 2020-05-20
@SpectreZ

Yes, the problem was in the template, but different.
By default, in the menu item traversal class, which extends the Walker_Nav_menu class, the condition for determining the item as active was as follows:

if(in_array('current-menu-item', $item->classes)) ...

I had to expand it by adding additional checks. I don’t know how correct this is, perhaps WP should have such features by default. I added these conditions:
(strpos($item->url, $post->post_name) > 0) || // для стандартных страниц
(strpos($item->url, $post->post_type) > 0) || // для записей пользовательских типов
(get_the_category($post)[0]->name == $item->title) // для стандартных записей рубрики

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question