Skip to content

Instantly share code, notes, and snippets.

@jin0x
Forked from atsea/submenu-descrption.php
Created April 21, 2020 12:09
Show Gist options
  • Save jin0x/c45bf325e19f70220c1e33347a3c4302 to your computer and use it in GitHub Desktop.
Save jin0x/c45bf325e19f70220c1e33347a3c4302 to your computer and use it in GitHub Desktop.

Revisions

  1. @atsea atsea created this gist May 6, 2015.
    67 changes: 67 additions & 0 deletions submenu-descrption.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    <?php
    /**
    * start_lvl and end_lvl
    * How can I add parent menu description to my Wordpress menu?
    * http://stackoverflow.com/questions/29251897/how-can-i-add-parent-menu-description-to-my-wordpress-menu
    *
    * start_el
    * * How to add menu descriptions to wordpress theme
    * http://www.wpbeginner.com/wp-themes/how-to-add-menu-descriptions-in-your-wordpress-themes/
    */
    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_el(&$output, $item, $depth, $args) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

    $class_names = $value = '';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    $class_names = ' class="' . esc_attr( $class_names ) . '"';

    $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

    $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
    $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
    $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
    $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    global $description;
    $output .= "\n$indent<ul class='sub-menu'><span class=\"sub\">".$description."</span></li>\n";
    }
    function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "$indent</ul>\n";
    }
    }
    ?>




    /**** add this to header.php ****/
    <?php
    $walker = new submenu_walker;

    $primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'walker' => $walker, 'echo' => false ) );
    ?>