display menu
Opened this issue · 1 comments
SGD-DEV commented
hi,
what is the code for display menu in frontend ?
MrZyr0 commented
Hey!
I had the same question. So, for the others who ask it too, here is my simple block created :
<?php
// Create class attribute allowing for custom "className" and "align" values.
$className = 'menu-block';
if ( ! empty( $block['className'] ) ) {
$className .= ' ' . $block['className'];
}
if ( ! empty( $block['align'] ) ) {
$className .= ' align' . $block['align'];
}
$menuId = get_field( 'menu' ) ?: null;
function generateMenuMarkup( $menu_id ) {
if (!is_string($menu_id))
return 'An error has occurs on generation. Excepted menu ID as string type, given (' . gettype($menu_id) . ') ' . $menu_id;
$menuData = wp_get_nav_menu_items( $menu_id );
$menuMarkup = '<ul id="navbar-menu" class="menu">';
foreach ( $menuData as $menuElement ) {
if ( $menuElement->menu_item_parent !== '0' ) {
return 'This bloc "menu-block" doesn\'t support submenu !';
}
$menuMarkup .= '<li class="menu-item' . ($menuElement->url === get_permalink(get_the_ID()) ? ' current-menu-item' : '') . '">' .
'<a href="' . $menuElement->url . '">' . $menuElement->title . '</a>' .
'</li>';
}
$menuMarkup .= '</ul>';
return $menuMarkup;
}
?>
<div class="<?php echo esc_attr( $className ); ?>">
<?php echo generateMenuMarkup( $menuId ) ?>
</div>