- This topic is empty.
-
AuthorPosts
-
October 4, 2022 at 8:30 pm #287
Billy
Guestwondering if I can limit only X amount of menu link items using some type of PHP function or filter in the WP theme
October 4, 2022 at 8:30 pm #288Keith
Guestone snippet
https://www.isitwp.com/limit-amount-of-menu-items/
<?php
add_filter( 'wp_nav_menu_objects', 'mytheme_menufilter', 10, 2 );
function mytheme_menufilter($items, $args) {
// want our MAINMENU to have MAX of 7 items
if ( $args->theme_location == 'mainmenu' ) {
$toplinks = 0;
foreach ( $items as $k => $v ) {
if ( $v->menu_item_parent == 0 ) {
// count how many top-level links we have so far...
$toplinks++;
}
// if we've passed our max # ...
if ( $toplinks > 7 ) {
unset($items[$k]);
}
}
}
return $items;
}
?>October 17, 2022 at 9:58 am #470Michelle
GuestThat snippet is nice but it would be cool if users were aware that only 1 link is allowed, there is no messaging.
November 28, 2022 at 5:12 am #662Daniel
GuestThat snippet is nice but it would be cool if users were aware that only 1 link is allowed, there is no messaging.
I guess they find out when they refresh the design lol
November 28, 2022 at 5:13 am #663Joan
GuestHoverCraft theme is using this type of snippet to limit the CTA buttons to a single menu link:
-
AuthorPosts