Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active November 30, 2021 18:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/c3a906031e61a4d175097fc356559475 to your computer and use it in GitHub Desktop.
Save mishterk/c3a906031e61a4d175097fc356559475 to your computer and use it in GitHub Desktop.
How to render WordPress menu items without a custom walker
<?php $menu_location = 'some_menu_location'; ?>
<?php if ( has_nav_menu( $menu_location ) ): ?>
<?php $menu_items = wp_get_nav_menu_items( wp_get_nav_menu_name( $menu_location ) ); ?>
<?php foreach ( $menu_items as $menu_item ): ?>
<a href="<?= esc_url( $menu_item->url ) ?>"
target="<?= esc_attr( $menu_item->target ?: '_self' ) ?>"
class="<?= esc_attr( implode( ' ', $menu_item->classes ) ) ?>"><?= esc_html( $menu_item->title ) ?></a>
<?php endforeach; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment