我正在使用此代码
在wordpress中获取菜单$items = wp_get_nav_menu_items( 'Top menu', $args );
它返回一个数组,其中包含以下项目
[4] => WP_Post Object
(
[ID] => 54
[post_author] => 1
[post_date] => 2014-02-19 20:36:53
[post_date_gmt] => 2014-02-19 20:36:53
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 54
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-19 20:36:53
[post_modified_gmt] => 2014-02-19 20:36:53
[post_content_filtered] =>
[post_parent] => 7
[guid] => http://localhost:8888/eastleigh/?p=54
[menu_order] => 6
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 54
[menu_item_parent] => 27
[object_id] => 46
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/eastleigh/about/menus/
[title] => Menus
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
看起来很好,但是当我进入wordpress时,页面没有该ID,实际上没有页面可以做(我在管理员中搜索页面来检查page_id)。但它确实匹配object_id(46)
如果我转出get_pages数组,则同一页面具有当前id(46)
[5] => WP_Post Object
(
[ID] => 46
[post_author] => 1
[post_date] => 2014-02-19 20:35:25
[post_date_gmt] => 2014-02-19 20:35:25
[post_content] =>
[post_title] => Menus
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => menus
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-19 20:35:25
[post_modified_gmt] => 2014-02-19 20:35:25
[post_content_filtered] =>
[post_parent] => 7
[guid] => http://localhost:8888/eastleigh/?page_id=46
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
有人可以解释一下我在这里缺少什么吗?
谢谢
答案 0 :(得分:1)
导航菜单项ID与帖子(页面)ID不同。您正在寻找的可比较ID是您已经指出的object_id是相同的。
答案 1 :(得分:0)
Wordpress菜单在技术上是“帖子”他们有自己的ID和一切(如果你不相信我去检查你的wp数据库中的wp_posts表)。菜单,页面和帖子之间的区别是小数据库值“post_type”
如果您要将菜单放入HTML,请改用以下语法:
<?php
wp_nav_menu( array(
'menu' => 'MENU-NAME'
)
);
?>
如果您要构建自己的主题,请确保使用functions.php文件注册此菜单。
答案 2 :(得分:0)
晚了几年,但是这个结果与Google有关,所以我将其发布。
如果您使用的是wp-admin
中的WP菜单选项,请不要忘记使用侧边栏选项“页面” ,否则ID将永远不会匹配。
稍后,您只需检查$wp_query->post->ID
是否与$your_menu_object_item->object_id
相同