• Resolved masoud4x4

    (@masoud2018)


    I am designing a WordPress theme and I have encountered a strange issue with the menus. I do not use the cache plugin in WordPress and the browser cache has been checked and there is no problem with this issue. I am developing the theme on xampp. I have defined two positions in my theme, one for the main menu position and the other for the top menu position. If one or both of these positions are empty, values ??are displayed that I do not know where they are loaded from. I added a filter to the function file so that if the user leaves the menu position empty, a specific phrase is displayed in it so that the theme style does not get messed up, and this filter also has no effect on displaying these values!

    in functions.php

    function liberty_setup_theme(){

    //addind dynamic menus
    register_nav_menus(
    array(
    'main-menu' => __( '?????? ????? ????' ),
    'top-menu' => __( '?????? ????? ????? ????' )
    )
    );


    }
    add_action('after_setup_theme', 'liberty_setup_theme');
       // ?????? ????? ???? ????? ?????? ???????
       add_filter( 'wp_nav_menu', 'custom_default_menu', 10, 2 );
       function custom_default_menu( $nav_menu, $args ) {
           // ????? ???? ?? ??? ??? ???? ??? ? ??? ?????? ???? ???? ??? ???
           if ( empty( $nav_menu ) && $args->theme_location == 'top-menu' ) {
               return '<div class="default-menu">???? ??????? ???</div>';
           }
           return $nav_menu;
       }
    

    in header.php

    <div class="topbar-left">
    
                <nav class="top-menu-nav">
                    <?php wp_nav_menu(array('theme_location' => 'top-menu', 'container' => '', 'menu_class' => 'top-menu-class')); ?>
                </nav>
                <ul class="topbar-icons">
                    <li><i class="fas fa-shopping-bag"></i></li>
                    <li><i class="fas fa-search"></i></li>
                </ul>
            </div>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    WP does not display empty menus itself. If you are seeing content where none should exist, it could be due some other code such as one of your plugins. Note that if wp_nav_menu() is called with invalid parameters it could output some other defined menu. I don’t see anything wrong with your call, just noting behavior seen elsewhere.

    If a menu is empty, filter “wp_nav_menu” will never fire because wp_nav_menu() will return prematurely. If you want some kind of default content when empty, try the “pre_wp_nav_menu” filter. You’ll have to check for no menu items yourself since all that is passed are the same args passed to wp_nav_menu(). For example, call wp_get_nav_menu_object() and check the returned object’s “count” property. If it’s 0, return alternative content.

    Or, wp_nav_menu() will return false if a requested menu is empty, so your template code you could check the return value and conditionally output alternative content that way.

    Thread Starter masoud4x4

    (@masoud2018)

    Thank you for your detailed and comprehensive response. It was extremely helpful and practical, and I appreciate you taking the time to explain it so well.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.