admin管理员组文章数量:1130349
I have the following function in my project:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id);
}
The function is used in my theme like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link): ?>
<a href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endforeach; ?>
This currently returns all navigation items present in my menu - parent/top-level and sub navigation. I am wondering how to alter this to exclude all sub navigation items. I only want to display the parent/top-level items.
I have the following function in my project:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id);
}
The function is used in my theme like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link): ?>
<a href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endforeach; ?>
This currently returns all navigation items present in my menu - parent/top-level and sub navigation. I am wondering how to alter this to exclude all sub navigation items. I only want to display the parent/top-level items.
Share Improve this question edited Apr 26, 2018 at 17:53 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 26, 2018 at 17:50 LizLiz 1551 gold badge2 silver badges8 bronze badges 1 |2 Answers
Reset to default 3Let's take a look at wp_get_nav_menu_items code reference.
It takes two parameters:
$menu- (int|string|WP_Term) (Required) Menu ID, slug, name, or object,$args- (array) (Optional) Arguments to pass to get_posts().
So we can use get_posts args in here... And if we want to get only top-level posts, then post_parent arg comes useful...
So something like this should do the trick:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id, array('post_parent' => 0));
}
This worked for me :
function cr_get_menu_items($Your_menu_location)
{
$menuLocations = get_nav_menu_locations();
$YourmenuID = $menuLocations[$Your_menu_location];
$YourNavItems = wp_get_nav_menu_items($YourmenuID);
}
$Your_menu_location is a string variable representing the menu name like 'navigation_menu' or 'primary' depending on how you registered your menu in functions.php , The function is used in my theme like this:
<?php
$menuitems = cr_get_menu_items('navigation_menu') ;
foreach ( (array)$menuitems as $menuitem )
{
if (!$menuitem->menu_item_parent )
echo '<a class="nav-link" href="'.$navItem->url.'">'.$navItem->title.' </a>';
}
?>
I have the following function in my project:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id);
}
The function is used in my theme like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link): ?>
<a href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endforeach; ?>
This currently returns all navigation items present in my menu - parent/top-level and sub navigation. I am wondering how to alter this to exclude all sub navigation items. I only want to display the parent/top-level items.
I have the following function in my project:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id);
}
The function is used in my theme like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link): ?>
<a href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endforeach; ?>
This currently returns all navigation items present in my menu - parent/top-level and sub navigation. I am wondering how to alter this to exclude all sub navigation items. I only want to display the parent/top-level items.
Share Improve this question edited Apr 26, 2018 at 17:53 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 26, 2018 at 17:50 LizLiz 1551 gold badge2 silver badges8 bronze badges 1-
i don't believe the selected answer adequately answers the OP's question as worded... "Return only top-level navigation items" seems very clear to me to indicate nav menu items with no
menu_item_parent... the selected answer does provide a solution IF the question was "Return only top-level pages as navigation items"... @Aness answered below and seems to provide the solution best fitting the OP's question (even if it is disappointing to have to post-filter the menu items). MY QUESTION : can/should we reword the OP's question to better match the answer? – aequalsb Commented Mar 17, 2023 at 19:37
2 Answers
Reset to default 3Let's take a look at wp_get_nav_menu_items code reference.
It takes two parameters:
$menu- (int|string|WP_Term) (Required) Menu ID, slug, name, or object,$args- (array) (Optional) Arguments to pass to get_posts().
So we can use get_posts args in here... And if we want to get only top-level posts, then post_parent arg comes useful...
So something like this should do the trick:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id, array('post_parent' => 0));
}
This worked for me :
function cr_get_menu_items($Your_menu_location)
{
$menuLocations = get_nav_menu_locations();
$YourmenuID = $menuLocations[$Your_menu_location];
$YourNavItems = wp_get_nav_menu_items($YourmenuID);
}
$Your_menu_location is a string variable representing the menu name like 'navigation_menu' or 'primary' depending on how you registered your menu in functions.php , The function is used in my theme like this:
<?php
$menuitems = cr_get_menu_items('navigation_menu') ;
foreach ( (array)$menuitems as $menuitem )
{
if (!$menuitem->menu_item_parent )
echo '<a class="nav-link" href="'.$navItem->url.'">'.$navItem->title.' </a>';
}
?>
本文标签: functionsReturn only toplevel navigation items from a menu using wpgetnavmenuitems
版权声明:本文标题:functions - Return only top-level navigation items from a menu using wp_get_nav_menu_items 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749018195a2303980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


menu_item_parent... the selected answer does provide a solution IF the question was "Return only top-level pages as navigation items"... @Aness answered below and seems to provide the solution best fitting the OP's question (even if it is disappointing to have to post-filter the menu items). MY QUESTION : can/should we reword the OP's question to better match the answer? – aequalsb Commented Mar 17, 2023 at 19:37