Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter duzymaju

    (@duzymaju)

    Hello,

    The case is not a plugins conflict because I don’t use there any plugin (there is even no inactive one). Let’s check and let me know if functionality I mentioned works for you and if so, I will try to figure out what’s wrong in my case.
    I used to use 1.1.11 before and made upgrade to the newest one 1.1.12 – this bug exists in both.

    Best wishes!

    Plugin Author duzymaju

    (@duzymaju)

    Hello Luke,

    I’m sorry that I didn’t respond for two months. Let me improve myself. ??

    That’s my fast solution to your problem (but maybe someone find simpler one):

    1. Define protected/private variable for your walker class. This is an array with current items counters on every depth level. Because start_lvl method isn’t called before first item of 0 level, you have to fill 0 index of your array by 0 value.
    protected $_itemsNoByDepth = array( 0 => 0 );

    2. When you start next level you have to set level counter to 0 in start_lvl method. Because first sub-level have $depth value 0 you have add 1 to this value.
    $this->_itemsNoByDepth[$depth+1] = 0;

    3. Finally you have to iterate proper counter in every occurrence of start_el method. After it you can use this counter to add required classes as a new $classes array indexes. In your case it will be:

    $this->_itemsNoByDepth[$depth]++;
    if( $depth > 0 && $this->_itemsNoByDepth[$depth] % 2 ) {
     $classes[] = 'odd';
    }

    but if you’d like to add eg. odd/even classes and classes with consecutive items numbers, you can do sth like:

    $this->_itemsNoByDepth[$depth]++;
    $classes[] = 'item-no-' . $this->_itemsNoByDepth[$depth];
    $classes[] = 'item-' . ( $this->_itemsNoByDepth[$depth] % 2 ? 'odd' : 'even' );

    Remember to put above code between $classes and $class_names variable declarations.

    After these three steps your code should looks like that:

    class Menu_With_Description extends Walker_Nav_Menu
    {
    
            protected $_itemsNoByDepth = array( 0 => 0 );
    
        	function start_lvl(&$output, $depth) {
                $this->_itemsNoByDepth[$depth+1] = 0;
            	$indent = str_repeat("\t", $depth);
            	$output .= "\n$indent<ul class=\"sub-menu level-".$depth."\">\n";
        	}
        	function end_lvl(&$output, $depth) {
            	$indent = str_repeat("\t", $depth);
            	$output .= "$indent<div class=\"fix\"></div></ul><div class=\"fix\"></div>\n";
        	}
    
        /**
         * Start the element output.
         *
         * @param  string $output Passed by reference. Used to append additional content.
         * @param  object $item   Menu item data object.
         * @param  int $depth     Depth of menu item. May be used for padding.
         * @param  array $args    Additional strings.
         * @return void
         */
         function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output )
    {
        $id_field = $this->db_fields['id'];
        if ( is_object( $args[0] ) ) {
            $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
        }
        return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }
    
        function start_el(&$output, $item, $depth, $args)
        {
    
            $classes     = empty ( $item->classes ) ? array () : (array) $item->classes;
    
            $this->_itemsNoByDepth[$depth]++;
            if( $depth > 0 && $this->_itemsNoByDepth[$depth] % 2 ) {
                $classes[] = 'odd';
            }
    
            $class_names = join(
                ' '
            ,   apply_filters(
                    'nav_menu_css_class'
                ,   array_filter( $classes ), $item
                )
            );
    
            if ($args->has_children && $depth == 0){
            ! empty ( $class_names )
                and $class_names = ' class="'. esc_attr( $class_names ) . ' has_children"';
            }else{
             ! empty ( $class_names )
                and $class_names = ' class="'. esc_attr( $class_names ) . '"';
            }
    
            $output .= "<li id='menu-item-$item->ID' $class_names>" ;
            $attributes  = '';
    
            ! empty( $item->attr_title )
                and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
            ! empty( $item->target )
                and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
            ! empty( $item->xfn )
                and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
            ! empty( $item->url )
                and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';
    
            // insert description for top level elements only
            // you may change this
            $description = ( ! empty ( $item->description ) and 0 == $depth )
                ? '<span class="description">' . esc_attr( $item->description ) . '</span>' : '';
    
            $title = apply_filters( 'the_title', $item->title, $item->ID );
    if ( $depth == 0 ) {//top level items
                $item_output = $args->before
                ."<div class='parent'><div class='cat-icon'></div><div class='title-desc'>"
                . "<a $attributes>"
                . $args->link_before
                . $title
                . '</a><br>'
                . $args->link_after
                . $description
                . '</div></div>'
                . $args->after;
            }else{//everything else
            $item_output = $args->before
                . "<a $attributes>"
                . $args->link_before
                . $title
                . '</a> '
                . $args->link_after
                . $args->after;
            }
            // Since $output is called by reference we don't need to return anything.
            $output .= apply_filters(
                'walker_nav_menu_start_el'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
            );
        }
    
    }

    Plugin Author duzymaju

    (@duzymaju)

    Hello Dinesh,

    Thanks a lot for your notice. Today I added wp_page_menu support in new 1.4 plugin’s version. ??

    I omitted wp_page_menu simply because I never use menu based on pages hierarchy and I thouht that it’s rarely used. After your notice I started thinking about adding control panel and allow users to check if they’d like to use classes extension into wp_nav_menu, wp_page_menu or both but I found that it would be only unnecessary database querying because more classes in menu elements isn’t a bad thing and if someone have two different menu types and he don’t need additional classes in one of them, he simply doesn’t need to use them. ??

    In new plugin’s version there are also a number of other changes so I will be grateful for checking new version of plugin.

    If you have other suggestions connected with this plugin, I’ll wait for your information. ??

    Plugin Author duzymaju

    (@duzymaju)

    Hello flynsarmy,

    Thanks a lot for your notice. Today I corrected that strict standards warning’s cause in new 1.4 plugin’s version. ??

    There are also a number of other changes so I will be grateful for checking new version of plugin.

    If you have other suggestions connected with this plugin, I’ll wait for your information. ??

    Plugin Author duzymaju

    (@duzymaju)

    Hi controlb,

    Thanks for using my tiny plugin. I hope that it helps you in your daily work as a webmaster/webdeveloper. ??

    I’d like to ask you which classes are missing in your menu while using Events Manager categories? Did you mean “current-menu-item”, “current-menu-parent” and “current-menu-ancestor”? These three classes are added to standard menu items “natively” by WordPress and my plugin has nothing to do with it. MAJpage Menu Class Extender adds only first/last/parent/even/odd-menu-item classes operating on finished XML structure so e.g. if menu item has submenu in ul list, it always gets “parent-menu-item” class.

    However I installed Events Manager to check its categories behavior in menu and I noticed that there are no “current-menu-item”, “current-menu-parent” and “current-menu-ancestor” classes in it – this is Events Manager plugin problem and if you ask me about these three classes, you have to talk about it with Events Manager plugin’s author.

    Tell me if you mean something different and if I didn’t answer your question exactly.

    Best wishes!
    Wiktor

    Plugin Author duzymaju

    (@duzymaju)

    Unfortunately there is no way to provide this type of information because of plugin “architecture” – it don’t get any information about menu categories/pages etc. and even don’t use Walker_Nav_Menu; it only get XHTML code and put into class attributes basic information about parity and dependency of menu elements.
    But I thought about extend this plugin so maybe when I have more time I add this kind of functionality. ??

    I have simple solution: MAJpage Menu Class Extender. Maybe it’s not as fast and pure as above but it’s great solution for people who can’t programming – just install and turn on this plugin and use additional CSS classes in your menu

    • tags: for first, last, parent, even and odd
    • elements on every menu level.
Viewing 7 replies - 1 through 7 (of 7 total)