admin管理员组文章数量:1122846
I'm trying to get the current-menu-item-id
in any page.
I have used this solution, that brings me very close to the solution.
The problem I have is, that the function below is going through all the menus on the page, and not only the one specific menu, that I want it to go through:
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
{
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['wpse16243_title'] = $menu_item->ID;
}
}
return $sorted_menu_items;
}
...which means that if you link to the same page more than once in your menus, you can risk that this function returns the current-menu-item-id
from the wrong menu.
Is there any way that I can limit this function to only go through a specific menu and not all menus?
I tried to pass the specific menu items in the variable/parameter $sorted_menu_items
, but that seems not to work.
I'm trying to get the current-menu-item-id
in any page.
I have used this solution, that brings me very close to the solution.
The problem I have is, that the function below is going through all the menus on the page, and not only the one specific menu, that I want it to go through:
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
{
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['wpse16243_title'] = $menu_item->ID;
}
}
return $sorted_menu_items;
}
...which means that if you link to the same page more than once in your menus, you can risk that this function returns the current-menu-item-id
from the wrong menu.
Is there any way that I can limit this function to only go through a specific menu and not all menus?
I tried to pass the specific menu items in the variable/parameter $sorted_menu_items
, but that seems not to work.
- 1 if a page is present twice in the menu, the URL is the same for the 2 then it's not possible to know the difference. – mmm Commented Jul 19, 2016 at 9:20
- @user3199063, without knowing your use case for the id, it's difficult to give an anser. If instead of asking about how to modify that function you tell us what you intend we might come with other solutions. Have you tried using the filter nav_menu_item_id? – Luis Sanz Commented Jul 19, 2016 at 9:41
- @mmm, indeed WordPress has a function (_nav_menu_item_id_use_once) that prevents that the same id is used twice. The second time a menu item appears it will share the same link and classes, but it won't have an ID. I think it will still be possible to tell the difference using the menu ID and the item class. – Luis Sanz Commented Jul 19, 2016 at 9:42
- Luis Sanz, what i need is to be able to catch the current-menu-item-id that i clicked on (from that specific menu), in all pages – user3199063 Commented Jul 19, 2016 at 9:50
- Do you mean that when you navigate to a new page you need to know its referrer? – Luis Sanz Commented Jul 19, 2016 at 11:38
2 Answers
Reset to default 0This was the solution to my problem:
Then what I would do is to store the clicked anchor in a cookie using javascript and read it while loading the new page. $_SERVER['HTTP_REFERER'] won't be reliable here. – Luis Sanz Jul 19 at 12:33
You could pass second argument to your function, which store menu name. Changes that you have to do:
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects', 10, 2 );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items, $args )
In function you will get menu name by $args['menu']
so
if ( $menu_item->current ) {
should be changed to
if ( $menu_item->current && $args['menu'] == 'name_of_menu_you_want_to_search' ) {
本文标签: Getting the current menu item id from specific menu
版权声明:本文标题:Getting the current menu item id from specific menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307507a1933334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论