如何在wordpress中显示用户城市和州,以及个人资料图片

问题描述 投票:0回答:1

我正在为我的WordPress网站上的用户创建一个自定义配置文件页面。我已经能够轻松显示登录用户的用户名。但我遇到了几个问题:

1)我很难找到如何显示登录用户的城市和州。此信息存储在送货地址部分下的仪表板/用户/“用户名”中。

2)我无法弄清楚如何显示用户的个人资料图片。根据我的阅读,用户个人资料图片存储在Gravatar的服务器上......

我在左侧边栏上显示这个...这是我目前为止显示用户名的代码

侧边栏profile.php

<div id="sidebar-profile" class="sidebar">
    <div>
        <h4>
            <?php 
                $current_user = wp_get_current_user();
                echo $current_user->user_login;
            ?>
        </h4>
    </div>
    <?php dynamic_sidebar( 'profile' ); ?>
</div>

我尝试了一些不同的东西,但都失败了。

此外,我是一名新手Web开发人员,因此我的方法显示这些值可能会有一些错误。

php wordpress
1个回答
0
投票

您应该能够使用上面的代码获取用户的图像,如下所示:

<div id="sidebar-profile" class="sidebar">
<div>
    <h4>
        <?php 
            $current_user = wp_get_current_user();
            echo $current_user->user_login;
            echo get_avatar( $current_user->user_email, 32 );
        ?>
    </h4>
</div>
<?php dynamic_sidebar( 'profile' ); ?>
</div>

这将显示带有gravatar的<img>标记,还可以使用get_avatar()函数的其他参数,以允许您进一步自定义输出:https://codex.wordpress.org/Function_Reference/get_avatar

我假设您正在使用WooCommerce的用户的城市和州,如果是这样,那么您应该能够从用户发布meta获取此信息:

$shippingCity  = get_post_meta($current_user->ID, 'shipping_city', true);
$shippingState = get_post_meta($current_user->ID, 'shipping_state', true);
© www.soinside.com 2019 - 2024. All rights reserved.