D
D
dimaast20202020-04-20 13:19:37
WordPress
dimaast2020, 2020-04-20 13:19:37

wp_nav_menu remove li ul leave and add asset class for current page and child?

Friends, hello!

Help me to understand.

There was a task in wp_nav_menu to remove the wrapper ul, li and add an active class for the link.

I successfully completed this task.

Removed wrapper ul, li

<?php
                    $headerMenu = array(
                        'theme_location' => 'header_menu',
                        'container' => '',
                        'echo' => '',
                        'items_wrap' => '%3$s',
                        'depth' => 0
                    );
                    echo strip_tags( wp_nav_menu( $headerMenu ), '<a>' );
                ?>


And through function.php added class .active
function filter_nav_menu_link_attributes( $atts, $item, $args ) {
  if ( $args->theme_location === 'header_nav' ) {
    if ( $item->current ) {
      $atts['class'] .= 'active';
        };
        if ( $item->current_item_parent ) {
      $atts['class'] .= 'active';
        }
  }
  return $atts;
};
add_filter( 'nav_menu_link_attributes', 'filter_nav_menu_link_attributes', 10, 4 );


How about adding the .active class if a child page is open?

In Wordpress, it is implemented through the .current-page-ancestor class.

Prompt, please, with implementation. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dimaast2020, 2020-04-21
@dimaast2020

21 views and not one answer.
In principle, I figured it out myself, but, based on the topics I read, I got the impression that only easy questions are answered here. ))
People. Hare keep your tongue in your ass.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question