Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display menu #3

Open
SGD-DEV opened this issue Feb 12, 2020 · 1 comment
Open

display menu #3

SGD-DEV opened this issue Feb 12, 2020 · 1 comment

Comments

@SGD-DEV
Copy link

SGD-DEV commented Feb 12, 2020

hi,
what is the code for display menu in frontend ?

@MrZyr0
Copy link

MrZyr0 commented Dec 21, 2020

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants