0

I want logged-in user see her/his own author archive page link in top navigation menu,say (a link to a list of all her/his posts).

What I achieved so far is this:

function my_nav_menu_author_link( $menu )
{
    if( !is_user_logged_in() ){

        return $menu;

    }else{

        $link = get_author_posts_url( get_current_user_id() );

        $author_archive_link = 
        '<li>' .
            '<a href="' . $link . '" >' .
                __( 'My posts' ) .
            '</a>' .
         '</li>';

        $menu = $menu . $author_archive_link;

        return $menu;

    }
}

add_filter( 'wp_nav_menu_items', 'my_nav_menu_author_link');

It works fine, but the problem is that this menu link does not get the current_menu_item class.

How could I add this functionality, so that I would be able to style it when it goes active?

1

Finally I figured it out and I am posting the solution for anyone who may be interested in such a workaround:

function my_nav_menu_author_link( $menu )
{
    if( !is_user_logged_in() ){

        return $menu;

    } else {

        $link = get_author_posts_url( get_current_user_id() );

        $class = is_author() ? ' class="current-menu-item"' : '';

        $author_archive_link = 
        '<li' . $class . '>' .
             '<a href="' . $link . '" >' .
                 __( 'My posts' ) .
             '</a>' .
         '</li>';

        $menu = $menu . $author_archive_link;

        return $menu;

    }
}

add_filter( 'wp_nav_menu_items', 'my_nav_menu_author_link' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.