1

i would like the possibility to append a text(denoted in square brackets) to each sub-item in the menu like so:

Parent 1:

  • sub-item [sub item 1 of 5]
  • sub-item [sub item 2 of 5]
  • sub-item [sub item 3 of 5]
  • sub-item [sub item 4 of 5]
  • sub-item [sub item 5 of 5]
    • sub-item [sub item 1 of 2]
    • sub-item [sub item 2 of 2]

Parent 2:

  • sub-item [sub item 1 of 4]
  • sub-item [sub item 2 of 4]
  • sub-item [sub item 3 of 4]
  • sub-item [sub item 4 of 4]

the actual thing Im trying to do is a bit more complicated, but if I know how to find the current index of sub item along with the total amounts of items in a dropdown, I can do what I am looking for.

Can anyone help here? :)

edit - pasted in my current custom walker code which is based on the Foundationpress theme

class FoundationPress_top_bar_walker_custom extends Walker_Nav_Menu {


    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {

        $element->has_children = !empty( $children_elements[$element->ID] );
        $element->classes[] = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
        $element->classes[] = ( $element->has_children && $max_depth !== 1 ) ? 'has-dropdown' : '';

        parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }






    function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {


        $item_html = '';
        parent::start_el( $item_html, $object, $depth, $args ); 

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

       //custom menu-item field with the use of a plugin
        $value = get_post_meta( $object->ID, $key, true );


        if( in_array('label', $classes) ) {
            $item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html );
        }

        if($value[ 'menu-item-polyfield-menuitem-color' ]){
            $color = $value[ 'menu-item-polyfield-menuitem-color' ][0];
            $item_html = preg_replace('/(<a\b[^><]*)>/i', '$1 style="background: #'.$color.'">', $item_html);

        }

        $output .= $item_html;
    }






    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $output .= "\n<ul class=\"sub-menu dropdown\">\n";
    }

}
  • Are you creating a custom walker menu or just trying to append text to an existing wp_nav_menu? Not sure if this might help: codex.wordpress.org/Class_Reference/Walker#General_Menu_Example If not, maybe posting some of your code would help others answer you more efficiently. :-) – RachieVee Jul 2 '15 at 19:59
  • @RacheVee, Thank you for your reply :) I am using a custom walker. I have edited an example of the existing code. As you can see, I am using custom fields to let the wp-admin change the color of each parent item at will. I originally had a class-based algorithm in sass, that would automatically create a color for each subitem(which is gradually lightened to a fixed end value. Difference in color tint of each sub-item is calculated based on how many sub-items there are, such that the last piece is always the same tint, which is why knowing the total nr of sub-items is important) – Ntikki Jul 3 '15 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.