根据用户角色更改主题徽标

我正在使用flatsome主题。

我想根据当前用户的用户角色来更改徽标。

flatsome-child文件夹中,我已经访问了functions.php。

仅尝试更改徽标是行不通的,所以我尝试了一些代码,给了我nada更改。

我已经搜索了如何更改自定义徽标,但是我发现的是如何在Wordpress中进行更改,但是我需要根据用户角色使用两个不同的徽标。

<?
add_filter('get_custom_logo','helpwp_custom_logo_output',10);
function helpwp_custom_logo_output() {
    $html = '<a href="https://www.linkhere.com/" title="linkhere.com" rel="home">';
    $html .= '<img width="278" height="73" src="https://www.linkhere.com/wp-content/uploads/2019/11/linkhere-logo-custom.png" class="header_logo header-logo" alt="linkhere" scale="0">';
    $html .= '<img width="278" height="73" src="https://www.linkhere.com/wp-content/uploads/2019/11/linkhere-logo-custom.png" class="header-logo-dark" alt="linkhere" scale="0">';
    $html .= '</a>';

    $html = str_replace('header_logo','logo',$html );
    return $html;
}
?>
x357904377 回答:根据用户角色更改主题徽标

WordPress将用户角色作为功能存储在用户元表中。您可以这样设置页面模板的格式:

<?php if (current_user_can('administrator')) : ?>
<img src="admin_logo.png" />
<?php elseif (current_user_can('editor')) : ?>
<img src="editor_logo.png" />
<?php else : ?>
<img src="logo.png" />
<?php endif; ?>

在上方给出徽标图像的自定义路径

,

WordPress具有一个名为

的功能
current_user_can( string $capability )

https://developer.wordpress.org/reference/functions/current_user_can/

它返回TRUE或FALSE。

功能包括: ‘delete_user’,‘edit_user’,‘remove_user’,‘promote_user’,‘delete_post’,‘delete_page’,‘edit_post’,‘edit_page’,‘read_post’或‘read_page’

部分支持用户角色,例如“管理员”或“编辑”。

使用此功能,您可以执行以下操作:

if(current_user_can( 'read_post' )) {
    //show a header image
} else if(current_user_can( 'edit_post' )) {
    //show a header image
} else {
    //show standard header
}

关于汤姆

本文链接:https://www.f2er.com/3153059.html

大家都在问