在 Wordpress 菜单中显示登录用户名 [英] Displaying Logged-In User Name in Wordpress Menu

查看:53
本文介绍了在 Wordpress 菜单中显示登录用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Wordpress 与 UserPro 结合使用,并希望我的菜单显示登录用户的名字,并链接到用户个人资料页面.

I'm using Wordpress with UserPro, and want my menu to display the logged-in user's first name, linked to the user profile page.

问题是在我的菜单结构中,配置文件"菜单选项应该有一个包含编辑配置文件"、提交"和注销"的子菜单.

The problem is that in my menu structure, the "Profile" menu option is supposed to have a sub-menu containing "edit profile", "submit", and "logout".

这是我当前的代码:

/*earlier code, currently commented out, for function to
display username in menu using #profile_name# placeholder

function give_profile_name($atts){
echo userpro_profile_data('first_name', get_current_user_id());
}

add_shortcode('profile_name', 'give_profile_name');

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( '#profile_name#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['profile_name'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
            }    
        }
    }

    return $menu_items;
}  

end of earlier code */

//currently in use, unlinked code

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
    function my_custom_menu_item($items)
    {
        if(is_user_logged_in())
        {
            $user=wp_get_current_user();
            $name=$user->user_firstname;
            $items .= '<li>'.$name.'';
        }
        return $items;
    }

    ?>

我可以摆弄并尝试通过摆弄 Firebug 中的代码来尝试在菜单下添加子菜单,但这意味着手动编辑functions.php 中的所有链接,这会很乏味.

I can fiddle around and try to add the sub-menu under the menu by fiddling with the code from Firebug, but that would mean manual edits to all the links in the functions.php, which would be tedious.

我希望能够通过 Wordpress 菜单轻松添加、编辑、删除和重定向子菜单项.

I want to be able to add, edit, remove, and redirect the sub-menu items easily via the Wordpress menu.

请指教.

推荐答案

好的,我找到了一个解决方案(它可以用于任何主题,任何插件,因为它只使用核心 WordPress 功能).

Okay, I found a solution (and it can be used for any theme, with any plugin as it only uses core WordPress functions).

在菜单中,使用占位符命名要显示用户名的菜单项(例如:#profile_name#、#user#、#random# 等)

In the menu, name the menu item where you want the user's name to appear with a place-holder (such as: #profile_name#, #user#, #random#, etc.)

现在,将以下代码添加到您的子主题的functions.php:

Now, add the following code to the your child-theme's functions.php:

function give_profile_name($atts){
    $user=wp_get_current_user();
    $name=$user->user_firstname; 
    return $name;
}

add_shortcode('profile_name', 'give_profile_name');

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( '#profile_name#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['profile_name'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
            }    
        }
    }

    return $menu_items;
} 

如果您使用自己的占位符,请记住在上面的代码中将#profile_name# 替换为您的自定义占位符的名称.

In case you're using your own place-holder, remember to replace #profile_name# with the name of your custom place-holder in the code above.

抱歉,我误用了占位符"一词.

Apologies in case I've misused the term 'place-holder'.

这篇关于在 Wordpress 菜单中显示登录用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆