简体   繁体   中英

Change Logo by User Role in Wordpress

Hey guys I have been asked to brand the site for my company. We are using Wordpress for it and basically each User Role is a different group that needs to see a different logo on the site.

I am using Eduma by Thimpress as my theme. I feel like it should be as simple as checking what user_role is logged in and changed the logo image, but I don't know where to start to put that condition.

Any help or guidance is much appreciated!

Below is the code I think creates the logo:

<?php
add_action( 'thim_logo', 'thim_logo', 1 );
// logo
if ( !function_exists( 'thim_logo' ) ) :
    function thim_logo() {
        $thim_logo = get_theme_mod( 'thim_logo', false );
        $style     = '';
        if ( !empty( $thim_logo ) ) {
            if ( is_numeric( $thim_logo ) ) {
                $logo_attachment = wp_get_attachment_image_src( $thim_logo, 'full' );
                if ( $logo_attachment ) {
                    $src   = $logo_attachment[0];
                    $style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
                } else {
                    // Default image
                    // Case: image ID from demo data
                    $src   = get_template_directory_uri() . '/images/logo.png';
                    $style = 'width="153" height="40"';
                }
            } else {
                $src = $thim_logo;
            }
        } else {
            // Default image
            // Case: The first install
            $src   = get_template_directory_uri() . '/images/logo-sticky.png';
            $style = 'width="153" height="40"';
        }
        $src = thim_ssl_secure_url($src);
        echo '<a href="' . esc_url( home_url( '/' ) ) . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . ' - ' . esc_attr( get_bloginfo( 'description' ) ) . '" rel="home" class="no-sticky-logo">';
        echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
        echo '</a>';
    }
endif;
add_action( 'thim_sticky_logo', 'thim_sticky_logo', 1 );

// get sticky logo
if ( !function_exists( 'thim_sticky_logo' ) ) :
    function thim_sticky_logo() {
        $sticky_logo = get_theme_mod( 'thim_sticky_logo', false );
        $style       = '';
        if ( !empty( $sticky_logo ) ) {
            if ( is_numeric( $sticky_logo ) ) {
                $logo_attachment = wp_get_attachment_image_src( $sticky_logo, 'full' );
                if ( $logo_attachment ) {
                    $src   = $logo_attachment[0];
                    $style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
                } else {
                    // Default image
                    // Case: image ID from demo data
                    $src   = get_template_directory_uri() . '/images/logo-sticky.png';
                    $style = 'width="153" height="40"';
                }
            } else {
                $src = $sticky_logo;
            }

        } else {
            // Default image
            // Case: The first install
            $src   = get_template_directory_uri() . '/images/logo-sticky.png';
            $style = 'width="153" height="40"';
        }
        $src = thim_ssl_secure_url($src);
        echo '<a href="' . esc_url( home_url( '/' ) ) . '" rel="home" class="sticky-logo">';
        echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
        echo '</a>';
    }
endif;

在设置徽标的文件中,将此代码放在此处。

$user = wp_get_current_user(); if ( in_array( 'author', (array) $user->roles ) ) { //logo for author role }

That depends on the layout of the theme. Likely, it's in the header.php. The way to proceed is something like this:

  1. Create a child theme https://codex.wordpress.org/Child_Themes
  2. Find out what file outputs the link to the logo Probably header.php but that depends on the theme; ask the theme developer if unsure.
  3. Use for example wp_get_current_user() https://codex.wordpress.org/Function_Reference/wp_get_current_user
  4. Do a php switch on the outcome of that and output various links http://php.net/manual/en/control-structures.switch.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM