根据运行配置的用户名显示经过验证的徽章

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

任何人都可以帮助我修改此代码。我想根据作者用户名显示不同的验证徽章(图标)和颜色。运行配置文件。

看起来像这个配置文件config.php


return [
    'rahat' => [
        'color' => 'blue',
        'icon' => 'fa-check-circle'
    ],
    'jondoe' => [
        'color' => 'orange',
        'icon' => 'fa-check'
    ],
    'smith' => [
        'color' => 'purple',
        'icon' => 'fa-check'
    ],
];

这是我的代码


function verified_author() {
    $icon = "";
    $color = "";
    $verified = '';
    $username = get_the_author_meta('user_login');
    $users_username = $username;
    $config = get_stylesheet_directory()."/config.php";
    foreach ($config as $username) {
        if (in_array($username, $users_username)) {
            $icon = 'fa-check-circle';
            $color = 'blue';
            $verified = '<i class="'.$icon.' '.$color.'"></i>';
        }
    }
    return $verified;
}

echo the_author().verified_author();
php wordpress multidimensional-array user-roles badge
1个回答
0
投票

你最好像下面这样过滤 the_author 钩子

add_filter( 'the_author', function ($display_name)  {
    $config = array(
        'rahat' => [
            'color' => 'blue',
            'icon' => 'fa-check-circle'
        ],
        'jondoe' => [
            'color' => 'orange',
            'icon' => 'fa-check'
        ],
        'smith' => array(
            'color' => 'purple',
            'icon' => 'fa-check'
        ),
    ); 

    $verified_author = $display_name;
    foreach ($config as $author => $data) {
        if ($author === $display_name) { 
            $verified = '<i class="'.$data['icon'].' '. $data['color'].'"></i>';
            $verified_author = $verified . $display_name;
            break;
        }
    }

    return $verified_author;
});

在我看来,将所有用户数据存储在一个文件中并不是一个好主意。 相反,您可以为此功能添加自定义用户元数据。如果你对此一无所知,你可以使用 acf 插件

© www.soinside.com 2019 - 2024. All rights reserved.