如何在 Wordpress 中获取菜单项?

2022-01-06 00:00:00 php wordpress menu

我想获取我的 wp 主菜单的项目.我尝试了一些代码,但所有代码都只返回空白页.我不知道我必须使用哪种方法,也不知道需要在我的页面中包含任何其他页面?

有没有办法直接从数据库中获取菜单项而不使用 wp 方法和函数?我现在无法理解 wp 的表结构.所以我不知道表之间的关系.

谢谢.

解决方案

你可以试试这个

$menu_name = 'sidebar-menu';//菜单栏$locations = get_nav_menu_locations();$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );echo "

";打印_r($menuitems);echo "</pre>";

你会得到你的菜单对象

参考:http://wiki.workassis.com/wordpress-get-menu-数组/

I want to get items of my wp primary menu. I tried some code but all of them are returns only blank page. I don't know which method I must use and don't know need include any other page to my page?

And Is there any way to get menu items from database directly without using wp methods and functions? I couldn't understand table structure of wp for now. So I don't know relationships between tables exactly.

Thanks.

解决方案

You can try this

$menu_name = 'sidebar-menu'; //menu slug
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

echo "<pre>";
print_r($menuitems);
echo "</pre>";

You will get your menu Object

refer : http://wiki.workassis.com/wordpress-get-menu-array/

相关文章