Skip to content

Instantly share code, notes, and snippets.

@AnatoliyAkhmatov
Last active June 13, 2019 15:58
Show Gist options
  • Save AnatoliyAkhmatov/0e59b7947ed24a5fcbba5fc7a1fc4e4c to your computer and use it in GitHub Desktop.
Save AnatoliyAkhmatov/0e59b7947ed24a5fcbba5fc7a1fc4e4c to your computer and use it in GitHub Desktop.
simple 2 level menu
<?php
$menu_id = get_nav_menu_locations()['main'];
$items = wp_get_nav_menu_items($menu_id);
?>
<?php $children = false; ?>
<?php foreach ( (array) $items as $key => $item ): ?>
<?php if ($item->menu_item_parent == 0): ?>
<?php if ($children): ?>
<?php $children = false; ?>
</ul></li>
<?php endif; ?>
<li><a href="<?php echo $item->url ?>"><?php echo $item->title ?></a>
<?php else: ?>
<?php if (!$children): ?>
<?php $children = true; ?>
<ul>
<?php endif; ?>
<li><a href="<?php echo $item->url ?>"><?php echo $item->title ?></a>
<?php endif; ?>
<?php endforeach;?>
<?php if ($children): ?>
<?php $children = false; ?>
</ul></li>
<?php endif; ?>
simple
<?php
$menu_id = get_nav_menu_locations()['social'];
$items = wp_get_nav_menu_items( $menu_id );
?>
<?php foreach ( (array) $items as $key => $item ): ?>
<li><a href="<?php echo $item->url ?>"><?php echo $item->title ?></a></li>
<?php endforeach;?>
<?php
$menu_id = get_nav_menu_locations()['my-style'];
$items = wp_get_nav_menu_items( $menu_id );
// for ($x = 0, $length = count($item->classes); $x < $length ; $x++) { echo ' ' . $item->classes[$x]; } // вывод классов
if( $items ):
?>
<div class="col-lg-6">
<article class="my-style">
<h2><?=wp_get_nav_menu_object( $menu_id )->name ?></h2>
<ul>
<?php foreach ( (array) $items as $key => $item ): ?>
<li><i class="fa <?php
for ($x = 0, $length = count($item->classes); $x < $length ; $x++) { echo ' ' . $item->classes[$x]; }
?> fa-fw" aria-hidden="true"></i></i><?= $item->title ?></li>
<li><a href="<?php echo item->url ?>"></a></li>
<?php endforeach;?>
</ul>
</article>
</div>
<?php endif; ?>
with submenu
<?php
$args = array (
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$menu_name = 'header_menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, $args );?>
<nav class="sf-iconMenu">
<ul id="menu-topnav" class="sf-menu_mod">
<?php
foreach ( $menuitems as $item ) :
// get page id from using menu item object id
$id = get_post_meta( $item->ID, '_menu_item_object_id', true );
$page = get_page( $id );
$title = $page->post_title;
$link = get_page_link( $id );
// item does not have a parent so menu_item_parent equals 0 (false)?>
<?php if ( $item->menu_item_parent == "0" ):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID;
switch (strtolower($title)){
case 'home': $icon='home.png';
break;
case 'about us': $icon='about_us.png';
break;
case 'services': $icon='services.png';
break;
case 'products': $icon='products.png';
break;
case 'energy solutions': $icon='energy_solns.png';
break;
case 'contact us': $icon='contact_us.png';
break;
default: $icon='';
}
?>
<li class="item">
<a href="<?php echo $link; ?>" class="title">
<img src="<?php echo bloginfo( 'template_url' ).'/images/icons/'.$icon; ?>" />
<span><?php echo $title; ?></span>
</a>
<?php endif;?>
<?php if ( $parent_id == $item->menu_item_parent ):?>
<?php if ( !$submenu ): $submenu = true; ?>
<ul class="sub-menu" par_id="<?php echo $parent_id; ?>" count="<?php echo $count; ?>">
<?php endif; ?>
<?php
// Retrieve second level menu through appropriate query
$args = array (
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'child_of' => $parent_id,
'parent' => -1,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages_under = get_pages($args); ?>
<li class="item">
<a href="<?php echo $link; ?>" class="title"><?php echo $title; ?></a>
<?php if ( count($pages_under)>0 ): ?>
<ul class="sub-menu2">
<?php foreach($pages_under as $page):
$id = $page->ID;
$title = $page->post_title;
$link = get_page_link($id); ?>
<li class="item">
<a href="< ?php echo $link; ?>" class="title"><?php echo $title; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?>
</ul>
<?php $submenu = false; // endif; ?>
<?php // if ( !$menuitems[ $count + 1 ]->menu_item_parent || !$menuitems[ $count + 1 ]->menu_item_parent==0 ): ?>
</li>
<?php endif; ?>
<?php endif; ?>
<?php $count++; endforeach; ?>
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment