Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wordpress mega menu - additional request WordPress

  • SOLVED

Hello
I was using this site to get a wp driven mega menu.
here:
http://www.wpquestions.com/question/showChronoLoggedIn/id/9521
I need a small additional stuff to be developed on that menu.
right now the mega menu displays 5 items in each column.
I can easily set it to 10 but I need 10 only in the first menu.
all others should stick with 5 in each row
this is the working code:

<?php $menu_name = 'top_menu';
$menu = wp_get_nav_menu_object($menu_name);
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );
?>
<ul class="nav top-nav">
<?php
$count = 0;
$submenu = false;
$i=0;
$output = '';
foreach( $menuitems as $item ){
$link = $item->url;
$title = $item->title;

//if no parent
if ( !$item->menu_item_parent ) {
$parent_id = $item->ID;
$output .= '<li>
<a href="'.$link.'">'.$title.'</a>';
}

if ( $parent_id == $item->menu_item_parent ) {

if ( !$submenu ) {
$submenu = true;
$output .= '<div class="mega-menu-section">
<div class="wrapper">
<ul class="drop-dow">';
}

$i++;
$output .= '<li>
<a href="'.$link.'">'.$title.'</a>
</li>';

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ) {
$output .= '</ul>
</div>
</div>';
$submenu = false;
$i=0;
} else if($i==5) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
}
}

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ) {
$output .= '</li>';
$submenu = false;
}
$count++;
}
echo $output; ?>


please let me have a working code which will display <strong>10 items in each column
in the first mega menu only</strong>
and 5 for all the rest

Thanks

Answers (3)

2014-09-04

Sébastien | French WordpressDesigner answers:

<?php
$menu_name = 'test';
$menu = wp_get_nav_menu_object($menu_name);
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) ); ?>
<ul class="nav top-nav">
<?php
$count = 0;
$submenu = false;
$i=0;
$first=true;
$output = '';
foreach( $menuitems as $item ){
$link = $item->url;
$title = $item->title;
if ($i==10) {$i=0;$first=false;}

//if no parent
if ( !$item->menu_item_parent ) {
$first=true;
$parent_id = $item->ID;
$output .= '<li>
<a href="'.$link.'">'.$title.'</a>';
}

if ( $parent_id == $item->menu_item_parent ) {

if ( !$submenu ) {
$submenu = true;
$output .= '<div class="mega-menu-section">
<div class="wrapper">
<ul class="drop-dow">';
}

$i++;
$output .= '<li>
<a href="'.$link.'">'.$title.$i.'</a>
</li>';

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ) {
$output .= '</ul>
</div>
</div>';
$submenu = false;
$i=0;
} else if(($i==10) && ($first==true)) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
$first=false;
} else if(($i==5) && ($first==false)) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
}
}

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ) {
$output .= '</li>';
$submenu = false;
}
$count++;
}
echo $output; ?>
</ul>


hamergil comments:

Hi Sebastien
It is not working
it only gives the first UL 10 items
but each mega menu has 10 items
+ there is a number associated with each link suddenly..


Sébastien | French WordpressDesigner comments:

ah ok !
the good code :

<?php
$menu_name = 'test';
$menu = wp_get_nav_menu_object($menu_name);
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) ); ?>
<ul class="nav top-nav">
<?php
$count = 0;
$submenu = false;
$i=0;
$first=true;
$output = '';
foreach( $menuitems as $item ){
$link = $item->url;
$title = $item->title;
if ($i==10) {$i=0;$first=false;}

//if no parent
if ( !$item->menu_item_parent ) {
$parent_id = $item->ID;
$output .= '<li>
<a href="'.$link.'">'.$title.'</a>';
}

if ( $parent_id == $item->menu_item_parent ) {

if ( !$submenu ) {
$submenu = true;
$output .= '<div class="mega-menu-section">
<div class="wrapper">
<ul class="drop-dow">';
}

$i++;
$output .= '<li>
<a href="'.$link.'">'.$title.$i.'</a>
</li>';

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ) {
$output .= '</ul>
</div>
</div>';
$submenu = false;
$first=false;
$i=0;
} else if(($i==10) && ($first==true)) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
} else if(($i==5) && ($first==false)) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
}
}

if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ) {
$output .= '</li>';
$submenu = false;
}
$count++;
}
echo $output; ?>
</ul>


hamergil comments:

it is working now
but the number still exist next to each link
can you remove the numbers?


Sébastien | French WordpressDesigner comments:

sorry :)
juste replace .$title.$i.
by .$title.


hamergil comments:

I think I found the solution:
Removing the $i variable..


2014-09-04

Arnav Joy answers:

can you show me site , where this menu is currently ?


Arnav Joy comments:

Please try this code

http://pastie.org/9526640


hamergil comments:

Hi Arnav
I was checking the code
Not working for me
you have the site link right?
let me know
Thanks


hamergil comments:

http://www.ida-home.co.il

2014-09-04

Dbranes answers:

Hi, you could try to replace:

} else if($i==5) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
}



with

} elseif( $first && $i == 10 ) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
$first = false;

} elseif( ! $first && $i == 5 ) {
$output .= '</ul>
<ul class="drop-dow">';
$i=0;
}


and

$first = true;


at the top of your code.


hamergil comments:

Hi Dbranes
I checked your code
unfortunately it didn't work
it showed 10 only in the first column
but I want all columns in the first mega menu
not the very first column

Thanks