是否将菜单项添加到登录用户作者页面?

时间:2016-11-25 作者:Manny

我正在尝试添加一个名为“我的帖子”或“我的个人资料”的菜单项,它会将登录的用户带到他们的作者页面

1 个回复
SO网友:Ismail

您可以使用wp_nav_menu_items 对此进行筛选,并检查用户是否已登录,并使用get_author_posts_url($autho_id) 正如bravokeyl所建议的:

add_filter(\'wp_nav_menu_items\', function( $items, $args ) {
    $index = \'top-menu\'; // menu index key, if you\'re not sure then var_dump( $args->theme_location )
    global $current_user;
    if ( $current_user->ID && $index === $args->theme_location ) {
        $items .= sprintf(
            \'<li id="menu-item-my-posts" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-my-posts menu-item-my-posts"><a href="%s">%s</a></li>\',
            get_author_posts_url( $current_user->ID ),
            \'My Posts\'
        );
    }
    return $items;
}, 10, 2);
希望这有帮助。

相关推荐

当作者ID和用户ID匹配时,is_Author(Get_Current_User_id())返回FALSE

在循环内部,作为登录到localhost测试站点时$user->ID == 1 功能is_author(get_current_user_id()) 退货false 什么时候get_the_author_meta(\'ID\') 返回1。因此,永远不会执行以下条件(is_user_logged_in() 退货true):if( is_user_logged_in() && is_author(get_current_user_id()) ) { // do stuff