0

I'm attempting to integrate the custom-logo functionality introduced in 4.5 and I'm running into some issues with the Customizer interface.

I'm checking has_custom_logo and, if it's false, I'm providing a text version of the site name and description as a fallback. It works perfectly fine when you add a custom logo. However, when you "Remove" it, it appears has_custom_logo still returns true.

Example:

if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) {
    the_custom_logo();
} else {
    echo 'Fallback';
}

Anyone else had any luck with something similar?

2 Answers 2

1

You have it around the wrong way has_custom_logo() will return false if there is no logo. function_exists( 'the_custom_logo' ) however will return true if you are using a version of Wordpress that support this function.

So if you seperate your if statement like below it will work.

if( function_exists( 'the_custom_logo' ) ) {
    if(has_custom_logo()) {
        the_custom_logo();
    } else {
        echo "No logo";
    }
}
2
  • What will happen here if the function doesn't exists - no fallback? Regarding the logic in the original question, it sounds like you're talking about the case A || B but not A && B? In general A && B is only true if both A and B are true. So function_exists( 'the_custom_logo' ) && has_custom_logo() is true if only both are true.
    – birgire
    Commented Jun 23, 2016 at 7:56
  • I ended up using get_theme_mod( 'custom_logo', false ) instead of the_custom_logo() because I feel like it's closer to the original functionality of my function prior to 4.5. I do however agree that I should have started out checking for support with function_exists( 'the_custom_logo' ) separately. I was just trying to be efficient.
    – Jeremy
    Commented Jun 24, 2016 at 11:43
2

I used similar code to that in the question, using an image as a fallback, and works fine. Below 4.5 displays the fallback image and above 4.5 displays the custom logo, if one is set. If there is no custom logo set or it is removed, it displays the fallback image.

<?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) : ?>
    <?php the_custom_logo(); ?>
<?php else : ?> 
    <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" title="<?php bloginfo( 'name' ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png" alt="<?php bloginfo( 'name' ); ?>" width="100" height="50" /></a></h1>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.