$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Getting URL value from menu item by ID|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Getting URL value from menu item by ID

matteradmin8PV0评论

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="URL">Exhibitions</a>
</li>

I can't seem to find anything in the direction...

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="URL">Exhibitions</a>
</li>

I can't seem to find anything in the direction...

Share Improve this question asked Jun 22, 2016 at 12:20 Eric MitjansEric Mitjans 1872 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far