WordPress - add second logo to customizer

4k views Asked by At

I want to add to my theme different logo that will be used on sticky header when user scrolls the page.

I tried to do it like here: https://wordpress.stackexchange.com/a/256985/185092

I edit Underscores theme so I added the code to customizer.php and the whole function looks like this:

function mytheme_customize_register( $wp_customize ) {
    $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
    $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
    $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

    if ( isset( $wp_customize->selective_refresh ) ) {
        $wp_customize->selective_refresh->add_partial(
            'blogname',
            array(
                'selector'        => '.site-title a',
                'render_callback' => 'mytheme_customize_partial_blogname',
            )
        );
        $wp_customize->selective_refresh->add_partial(
            'blogdescription',
            array(
                'selector'        => '.site-description',
                'render_callback' => 'mytheme_customize_partial_blogdescription',
            )
        );
    }

    $wp_customize->add_setting('sticky_header_logo');
    $wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'sticky_header_logo', array(
        'label' => 'Sticky Header Logo',
        'section' => 'title_tagline', 
        'settings' => 'sticky_header_logo',
        'priority' => 8 
    )));
}
add_action( 'customize_register', 'mytheme_customize_register' );

and in header.php I have:

            <div class="site-branding">
                <?php the_custom_logo(); ?>
            </div>
            <div class="site-branding-alternative">
                <?php get_theme_mod( 'sticky_header_logo' ) ?>
            </div>

I will use display none and block to show one logo or another but now css is not done so both logos should appear. The problem is, I have possibility to add the second logo in Appearance - Customize - Site identity but it is not shown on the page.

What did I do wrong?

edit:

<?php var_dump(get_theme_mods()); ?>

gave me such a code:

array(7) {
  [0]=>bool(false) 
  ["nav_menu_locations"]=>array(1) {
    ["menu-1"]=>int(2)
  }
  ["custom_css_post_id"]=>int(-1) 
  ["header_image"]=>string(75) "http://localhost/whites/wp-content/uploads/2020/07/cropped-header-image.png" 
  ["header_image_data"]=>object(stdClass)#1138 (5) {
    ["attachment_id"]=>int(15) ["url"]=>string(75) "http://localhost/whites/wp-content/uploads/2020/07/cropped-header-image.png" ["thumbnail_url"]=>string(75) "http://localhost/whites/wp-content/uploads/2020/07/cropped-header-image.png" ["height"]=>int(473) ["width"]=>int(1000)
  }
  ["custom_logo"]=>int(18) 
  ["sticky_header_logo"]=>string(63) "http://localhost/whites/wp-content/uploads/2020/07/logo-3-1.png"
}
2

There are 2 answers

2
FluffyKitten On BEST ANSWER

Now that we have gone through the problem step by step, we know the code in customizer.php is working because :

  • get_theme_modsreturns your logo file so the problem's not the database or theme mods.
  • var_dump shows that the logo file is saved in the sticky_header_logo array key as expected.

The problem:

This means that the problem must be in header.php so we have narrowed down where to start looking for the coding problem. The marvels of debugging :) If we look at the code you are using to show the second logo:

<div class="site-branding-alternative">
    <?php get_theme_mod( 'sticky_header_logo' ) ?>
</div>

You are using the get_theme_mod to get the value of sticky_header_logo which is correct... but you are not displaying the result. This function doesn't output the logo in the same way that the_custom_logo does, it just returns the url.

To fix the problem:

You need to create the <img> tag using the url returned from get_theme_mod and echo it to the screen, e.g.:

<div class="site-branding-alternative">
    <?php 
    $sticky_logo_url = get_theme_mod( 'sticky_header_logo' );
    if ($sticky_logo_url )
       echo '<img src="'.$sticky_logo_url.'" alt = "logo alt test" class="sticky_logo_class">';
    ?>
</div>

Reference:

0
Muhammad Fahad On

Use the hook to add a transparent logo uploader

add_action('customize_register', 'transparent_logo_customize_register');

function transparent_logo_customize_register($wp_customize){}

Inside the function you can add as many fields as you want

$wp_customize->add_setting('transparent_logo');
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'transparent_logo', array(
    'label'    => __('Transparent Logo', 'store-front'),
    'section'  => 'title_tagline',
    'settings' => 'transparent_logo',
    'priority'       => 4,
)));