How can I add parent menu description to my Wordpress menu?

前端 未结 2 874
遥遥无期
遥遥无期 2020-12-20 02:18

I\'ve entered the description into the parent menu items in Wordpress, but they\'re not showing on my theme.

I know that a walker class can be used to make changes t

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 02:56

    I managed to get the menu to display as before, whilst adding the parent menu description exactly where I wanted by simply just adding to what I already had.

    I already had a walker that created the sub-menu image container, and the description container

    // Submenu walker to add image
    class submenu_walker extends Walker_Nav_Menu {
        function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent\n";
        }
    }
    

    Then I managed to find a function that uses start_el and could assign the description to a variable, but not output it, then just output the $item_output as normal.

    function add_menu_description( $item_output, $item, $depth, $args ) {
        $description = __( $item->post_content );
        return $item_output;
    }
    add_filter( 'walker_nav_menu_start_el', 'add_menu_description', 10, 4);
    

    Of course now I needed to use $description within my other submenu walker function, so I just created a global variable in both, and the output is exactly what I'm after!

    FINAL OUTPUT

    function add_menu_description( $item_output, $item, $depth, $args ) {
        global $description;
        $description = __( $item->post_content );
        return $item_output;
    }
    add_filter( 'walker_nav_menu_start_el', 'add_menu_description', 10, 4);
    
    // Submenu walker to add image
    class submenu_walker extends Walker_Nav_Menu {
        function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            global $description;
            $output .= "\n$indent\n";
        }
    }
    

提交回复
热议问题