0

This code works, but it's going to get huge! I have 150 "a href" to make and this is going to get rough, lol

Each role has a custom url.

How can I make this cleaner or smaller?

    if ( ! function_exists( 'add_extra_item_to_nav_menu' ) ){

    global $user_ID;

    function add_extra_item_to_nav_menu( $items, $args ) {
       if (current_user_can('administrator') && is_user_logged_in() && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-A/" class="navShopNow fungula">SHOP NOW ADMIN</a></li>';
       }
 
       // School ROLEABC
       elseif (current_user_can('ROLEABC') && is_user_logged_in && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-B/" class="navShopNow fungula">SHOP NOW ABC</a></li>';
       }
       // School ROLEXYZ
       elseif (current_user_can('ROLEXYZ') && is_user_logged_in && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-C/" class="navShopNow fungula">SHOP NOW XYZ</a></li>';
       }
 
       return $items;
    }
 
   add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 150, 2 );
 }
6
  • Nope, because it's exactly the same code. Commented Apr 19, 2022 at 15:27
  • Please edit your question to clarify what the problem is. You say you have 150 links; are you concerned about building 150 if /elseif for your roles? Are the links and the roles consistent (eg, it's 150 links of the format /product-category/public/{school name} and the school name always matches with the same role)?
    – Pat J
    Commented Apr 19, 2022 at 15:48
  • 1
    this is more like a PHP question, but use e.g. array of arrays and loop over it with foreach and check, if user is logged in and the menu argument, early and only once, etc
    – birgire
    Commented Apr 19, 2022 at 15:49
  • Alternately, since you've already asked a question about this, you might be further ahead to edit that question instead, and provide detail on a) what you need to accomplish and b) where you're having problems.
    – Pat J
    Commented Apr 19, 2022 at 15:50
  • ps 150 items might not be a good fit for a menu wrt the user experience.
    – birgire
    Commented Apr 19, 2022 at 15:57

0

Browse other questions tagged or ask your own question.