3
27/11/2024 4:44 上午
主题启动器
Hi, I have a WooCommerce site and in the menu I want to show the username - instread of the My Profile / My Account for logged-in users.
How can I do it?
Maybe some code snippet in functions.php file?
Thanks
2 答案
3
27/11/2024 4:51 上午
In your functions.php (in your Child theme) add this code:
/* Use shortcode: [current_user_display_name] for menu and My account page */ function display_current_user_display_name() { // Get current user $user = wp_get_current_user(); // Check if the user is logged in if ( $user->exists() ) { return $user->display_name; } // Return a fallback for non-logged-in users return 'Profile'; } add_shortcode( 'current_user_display_name', 'display_current_user_display_name' );
your shortcode will be: [current_user_display_name]
Now you have to add the option to show shortodes in your WordPress menu.
So, in the functions.php again add this line of code:
add_filter('wp_nav_menu_items', 'do_shortcode');
Now you can use everywhere in your site the shortcode: [current_user_display_name]
cheers 🙂
2
27/11/2024 4:56 上午
If you are using Max Mega Menu, you do not need to add the option for adding the shortcode for your WP menu. Just use the Replacement (HTML) option.
Read THIS ARTICLE to learn more.