【问题标题】:Change "My Account" to "Username" WooCommerce sidebar将“我的帐户”更改为“用户名”WooCommerce 侧边栏
【发布时间】: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' )); 吗?
  • 我只是这样做了,但它不起作用!

标签: php wordpress woocommerce account userinfo


【解决方案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 );
}

参考文献:

【讨论】:

  • 感谢您分享您的答案,但它没有显示任何内容!
  • 错误是: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你有什么想法吗?
  • 我认为这与它无关!
猜你喜欢
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 2019-10-09
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
相关资源
最近更新 更多