I want to display a link on the Frontend, but the URL value changes over time.
It’d be very easy for me to just change it manually in the WordPress menu once (it’s also displaying there), and then automatically replace the URL value in the template file link.

But, is there any way to query and echo the URL of a specific Menu Item by ID?

<li id="menu-item-5020" class="menu-item menu-item-type-custom">
    <a title="Exhibitions" href="https://wordpress.stackexchange.com/questions/230449/URL">Exhibitions</a>
</li>

I can’t seem to find anything in the direction…

1 Answer
1

Guessing the only way you get it without loop through the menu, is getting the post meta directly.

Navigation menus are saved as custom post type into wp_posts, so you can get it with get_posts or get_post_meta.

If it’s a custom link menu item, the code below should do it. Where $menu_id is you menu item id.

get_post_meta($menu_id, '_menu_item_url', true);

I don’t know if it’s the best approach, but I think it’s a start.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *