V
V
Vladimir2021-09-30 09:07:43
WordPress
Vladimir, 2021-09-30 09:07:43

How to change the styles of menus and other elements in Wordpress?

Hello! I have a ready-made layout on Bootstrap. I decided to pull it on Wordpress, but the styles do not match. To rewrite for example the menu for a long time. Is it possible to change them in Wordpress. And if so, how?

I think the engine should be able to

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-09-30
@pLavrenov

These hooks close 99% of the cases.

## Добавление стилей для стандартного меню
add_filter('nav_menu_css_class', 'add_menu_list_item_class', 1, 3);
function add_menu_list_item_class($classes, $item, $args) {
    if (property_exists($args, 'li_class') && !$item->menu_item_parent) {
        $classes[] = $args->li_class;
    }
    if (property_exists($args, 'li_child_class') && $item->menu_item_parent) {
        $classes[] = $args->li_child_class;
    }
    return $classes;
}

add_filter( 'nav_menu_link_attributes', 'add_menu_link_class', 1, 3 );
function add_menu_link_class( $atts, $item, $args ) {
    if ( strpos( $atts['href'], home_url() ) === false ) {
        $atts['target'] = '_blank';
    }
    if (property_exists($args, 'a_class') && !$item->menu_item_parent) {
        $atts['class'] = $args->a_class;
    }
    if (property_exists($args, 'a_child_class') && $item->menu_item_parent) {
        $atts['class'] = $args->a_child_class;
    }
    return $atts;
}

add_filter( 'nav_menu_submenu_css_class', 'my_nav_menu_submenu_css_class', 1, 3);
function my_nav_menu_submenu_css_class($classes, $args, $depth) {
    if (property_exists($args, 'ul_child_class')) {
        $classes[] = $args->ul_child_class;
    }
    return $classes;
}

$args = array(
    'theme_location' => 'mobile',
    'container'=> false,
    'menu_id' => 'top-nav-ul',
    'items_wrap' => '<ul id="%1$s" class="nav navbar-nav %2$s">%3$s</ul>',
    'li_class' => '' // <======================!!!!!!!!!
);
wp_nav_menu($args);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question