Support

Account

Home Forums General Issues Menu Item from user profile field (Page Link)

Helping

Menu Item from user profile field (Page Link)

  • Hello, I have tried tons of snippets / tutorials to try programatically add items to a WP menu and can’t get it to work. I can get the link working in templates using this code:

    $cluburl = get_field('club_membership_profile', 'user_'.get_current_user_id());

    But I can’t figure out how to get that URL to be able to be dynamically populated in my menu. My PHP knowledge is pretty basic at this point, so it might be bad syntax or just a stupid mistake. I have tried code like this but it doesn’t work:

    function add_club_link($items, $args) {
    $cluburl = get_field('club_membership_profile', 'user_'.get_current_user_id());
    if (isset($cluburl)) {
     if( $args->theme_location == 'Primary Menu' ){
     $items .= '<li class="menu-item">'
     . 'test'.$cluburl.' end test'
     . '</li>';
     }
     }
     return $items;
    }
    add_filter('wp_nav_menu_items', 'add_club_link', 10, 2);

    Is this possible? Is there anyway to generate a menu item from this field? I have tried a ton of group/club plugins for my site and they do not accomplish what I want, so I am trying to do this using ACF. I’m not sure if my language is even correct trying to address this problem.

    Any help is much appreciated.

  • What type of field is “club_membership_profile”?

    Your code looks correct, for the most part, except if (isset($cluburl)) {. This will always be true and should be if ($cluburl) {. Other than this it depends on the field type. What your doing would work for a URL field.

    This may also be incorrect if( $args->theme_location == 'Primary Menu' ){. I think you need to test against the menu name, like “menu-1” rather than the menu label.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Menu Item from user profile field (Page Link)’ is closed to new replies.