将“我的帐户”更改为“用户名”WooCommerce 侧边栏

Posted

技术标签:

【中文标题】将“我的帐户”更改为“用户名”WooCommerce 侧边栏【英文标题】:Change "My Account" to "Username" WooCommerce sidebar 【发布时间】:2016-05-30 08:15:39 【问题描述】:

我想在登录后为用户显示,而不是默认单词“我的帐户”我想显示用户名,我尝试了此代码,但它没有显示任何内容!

它似乎无法识别位于以下文件中的变量$current_user:wp-content/themes/themeName/framework/functions/woo-account.php

printf( __( '%s', 'wpdance' ),$current_user->user_lastname);

原来是:

printf( __( 'My Account', 'wpdance' ));

我还尝试使用此代码获取所有内容:

<?php global $current_user;
  get_currentuserinfo();

  echo 'Username: ' . $current_user->user_login . "\n";
  echo 'User email: ' . $current_user->user_email . "\n";
  echo 'User level: ' . $current_user->user_level . "\n";
  echo 'User first name: ' . $current_user->user_firstname . "\n";
  echo 'User last name: ' . $current_user->user_lastname . "\n";
  echo 'User display name: ' . $current_user->display_name . "\n";
  echo 'User ID: ' . $current_user->ID . "\n";

?>

但是User first name:User last name: 是空的!

有人有什么建议或想法吗?

谢谢你!

【问题讨论】:

你试过printf( __( $current_user-&gt;user_lastname, 'wpdance' )); 吗? 我只是这样做了,但它不起作用! 【参考方案1】:

最好的方法是使用wp_get_current_user()(不需要任何全局变量)和一个确保用户登录的条件:

if ( is_user_logged_in() ) 
    $user_info = wp_get_current_user();
    $user_last_name = $user_info->user_lastname;
    printf( __( '%s', 'wpdance' ), $user_last_name );

或全名:

if ( is_user_logged_in() ) 
    $user_info = wp_get_current_user();
    $user_complete_name = $user_info->user_firstname . ' ' . $user_info->user_lastname;
    printf( __( '%s', 'wpdance' ), $user_complete_name );

参考文献:

Get currentuserinfo firstname and lastname Function Reference/wp get current user

【讨论】:

感谢您分享您的答案,但它没有显示任何内容! 错误是:Notice: La méthode du constructeur appelée pour WP_Widget dans mgwoocommercebrands_list_widget est obsolète depuis la version 4.3.0 ! Utilisez __construct() à la place. in /home/epicerymarket/public_html/wp-includes/functions.php on line 3718你有什么想法吗? 我明白了,你是对的,你的代码是正确的!所以我有另一个问题,我会发布关于它的新问题! @AbdelazizMirad 自 wordpress 4.3 以来,您没有使用正确的方法来制作小部件。但这是另一个问题(Une autre question)。在这里,您有一个法语解决方案:Notice: WP_Widget is deprecated since version 4.3.0。谢谢你:)【参考方案2】:

尝试调用

global $current_user;
get_currentuserinfo();

之前

printf( __( '%s', 'wpdance' ),$current_user->user_lastname);

见https://codex.wordpress.org/Function_Reference/get_currentuserinfo#Examples

你确定总是设置姓氏吗?如果$current_user-&gt;ID 至少返回一个值,您可能可以确保$current_user 有效。

在您的wp_config.php 中启用调试可能也有助于显示所有通知和错误:

define( 'WP_DEBUG', true );

见https://codex.wordpress.org/Debugging_in_WordPress

【讨论】:

谢谢你的回答,我试过了,出现了这个错误:Notice: The called constructor method for WP_Widget in mgwoocommercebrands_list_widget is deprecated since version 4.3.0! Use __construct() instead. in /home/classi33/public_html/wp-includes/functions.php on line 3718你有什么想法吗? 我认为这与它无关!

以上是关于将“我的帐户”更改为“用户名”WooCommerce 侧边栏的主要内容,如果未能解决你的问题,请参考以下文章

无法将 Apple ID 更改为 App Store

如何使用 CLI 命令更改 GCP 中的项目

在终端上切换 Github 帐户时遇到问题

尝试用 Woocommerce 购物车上的删除文本替换“x”

如何将 db 架构更改为 dbo

Magento 我的帐户布局 XML 问题