I am attempting to add up all the string lengths of each menu item I have so I can have a character limit for my entire navigation rather than a set limit on each item.
I am currently using the Walker_Nav_Menu in WordPress. I can get each individual length but I can't add them all together in one variable.
First I check to see if the item is a main header not a sub menu and then use strlen to get the length of the text.
class top_bar_walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
if( $item->menu_item_parent == 0 ) {
$length = strlen( $item->title );
}
var_dump($length);
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$classes[] = ($item->current) ? 'active' : '';
$classes[] = ($args->has_children) ? 'has-dropdown' : '';
$args->link_before = (in_array('section', $classes)) ? '<label>' : '';
$args->link_after = (in_array('section', $classes)) ? '</label>' : '';
$output .= (in_array('section', $classes)) ? '<li class="divider"></li>' : '';
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args ) );
$class_names = strlen(trim($class_names)) > 0 ? ' class="'.esc_attr($class_names).'"' : '';
$output .= ($depth == 0) ? $indent.'<li class="divider"></li>' : '';
$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 .= (!in_array('section', $classes)) ? '<a'.$attributes.'>' : '';
// Find out if menu item is top level. If character size is greater than 15, cut down string and add '..''
$item_output .= $args->link_before.($item->menu_item_parent == 0 && strlen( $item->title ) > 10 ) ? trim( substr( apply_filters( 'the_title', $item->title, $item->ID ), 0, 10 ) ).".." : apply_filters('the_title', $item->title, $item->ID);
$item_output .= $args->link_after;
$item_output .= (!in_array('section', $classes)) ? '</a>' : '';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
When I do a var dump of $length I get
int(38) NULL NULL int(32) NULL int(16) NULL int(18) NULL int(42) NULL int(20) NULL
But i am unable to add them together
Any help would be appreciated.