WPDIR
Everything and everyone WordPress

can you limit the number of links allowed in a WordPress menu?

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #287 Reply
    Billy
    Guest

    wondering if I can limit only X amount of menu link items using some type of PHP function or filter in the WP theme

    #288 Reply
    Keith
    Guest

    one 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;
    }
    ?>

    #470 Reply
    Michelle
    Guest

    That snippet is nice but it would be cool if users were aware that only 1 link is allowed, there is no messaging.

    #662 Reply
    Daniel
    Guest

    That 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

    #663 Reply
    Joan
    Guest

    HoverCraft theme is using this type of snippet to limit the CTA buttons to a single menu link:

    Stop fixing your WordPress theme, and focus on your business.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: can you limit the number of links allowed in a WordPress menu?
Last modified on October 4, 2022