douchu4048 2015-08-13 15:05
浏览 12

加起来整数php

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏