php 如何在 Wordpress 中获取菜单项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27656929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get Menu Items In Wordpress?
提问by user2674354
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?
我想获取我的 wp 主菜单的项目。我尝试了一些代码,但所有代码都只返回空白页。我不知道我必须使用哪种方法,也不知道需要在我的页面中包含任何其他页面?
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.
有没有办法直接从数据库中获取菜单项而不使用 wp 方法和函数?我现在无法理解 wp 的表结构。所以我不知道表之间的关系。
Thanks.
谢谢。
回答by HomAfromPTZ
If you know your menu location id (usually declared in functions.php by "register_nav_menus") you can use this snippet:
如果您知道您的菜单位置 ID(通常在 functions.php 中由“register_nav_menus”声明),您可以使用以下代码段:
// GET ALL MENU OBJECTS AT SPECIFIED LOCATION
function yourprefix_get_menu_items($location_id){
//$locations = get_registered_nav_menus();
$menus = wp_get_nav_menus();
$menu_locations = get_nav_menu_locations();
if (isset($menu_locations[ $location_id ]) && $menu_locations[ $location_id ]!=0) {
foreach ($menus as $menu) {
if ($menu->term_id == $menu_locations[ $location_id ]) {
$menu_items = wp_get_nav_menu_items($menu);
break;
}
}
return $menu_items;
}
}
Or more short version from codex:
或来自 Codex 的更短版本:
function yourprefix_get_menu_items($menu_name){
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
return wp_get_nav_menu_items($menu->term_id);
}
}
Then do everything you want with this array like so:
然后用这个数组做你想做的一切,像这样:
$menu_items = yourprefix_get_menu_items('sidebar-menu'); // replace sidebar-menu by desired location
if(isset($menu_items)){
foreach ( (array) $menu_items as $key => $menu_item ) {
...some code...
}
}
And here is link about all nav_menu data which you can select directly from database by mysql request: http://lasota.community.uaf.edu/2011/07/29/nav-menu-data-location-in-wordpress-3-2/
这是关于所有 nav_menu 数据的链接,您可以通过 mysql 请求直接从数据库中选择:http: //lasota.community.uaf.edu/2011/07/29/nav-menu-data-location-in-wordpress-3 -2/
回答by gaurav kumar
<?php
$menu = 'menu-name/menu-id';
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$items = wp_get_nav_menu_items( $menu, $args );
?>
回答by Bikesh M
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
你会得到你的菜单对象
回答by Marventus
You could do this:
你可以这样做:
add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
function user_nav_menu_items( $items ) {
/* do something with $items (string) */
return $items;
}
If you want it as an array, just explode it and implode it after you are done:
如果你想要它作为一个数组,只需在完成后爆炸它并内爆它:
add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
function user_nav_menu_items( $items ) {
$items_r = explode("<li", $items);
/* do something with $items_r (array) */
$items_new = implode("<li", $items_r);
return $items_new;
}
In both cases, you would to replace menuname
in the add_action hook for the name of the menu you are trying to get the items of.
在这两种情况下,您都将menuname
在 add_action 挂钩中替换您尝试获取项目的菜单的名称。
回答by jay.jivani
Use get_registered_nav_menus()function
使用get_registered_nav_menus()函数
<?php
$menus = get_registered_nav_menus();
foreach ( $menus as $location => $description ) {
echo $location . ': ' . $description . '<br />';
}