0

I asked this question and got a great reply - Associate Page with User - so I can now associate a post with a specific user as required.

What I want to do is include in the main site navigation a link to 'View Your Page' that displays the post that has been linked to the logged in user. If the user is not logged in or does not have a page associated to them then a simple message to say so. Any idea of how to do this?

Thanks.

0

1 Answer 1

0

While you could possibly add a filter to walker_nav_menu_start_el and have your callback function alter/recreate the menu item according to your needs, I would say the best and easiest solution is to implement you own menu walker, and supply that to wp_nav_menu. This should help get you started:

class WPSE_90204_Walker extends Walker_Nav_Menu {

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $user_id = get_current_user_id();
        $page_id = 1234; // ID of the "magic" page

        if ($user_id) {
            // Check if menu item links to page
            if ($item->object_id == $page_id) {
                $item->url = get_author_posts_url($user_id);
            }
        }

        return parent::start_el($output, $item, $depth, $args, $id);
    }
}

And then when calling wp_nav_menu:

wp_nav_menu(array(
    // Probably some other args here...
    'walker' => new WPSE_90204_Walker,
));

Using this method you can add an ordinary page to you menu. This page will be displayed when the user is not logged in, so you could place a warning message, registration/login form etc. there. If the user is logged in, the walker will compare the object_id (related post) for each menu item, and in case it matches the ID of your page it will point the URL of that menu item to the user profile page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.