2

I'm trying to use WordPress' new custom logo feature to accomplish the following:

  • Display a default/fallback logo.
  • If WordPress version supports custom logo, allow user to replace the default/fallback logo with a custom logo in the Customizer.
  • If WordPress version doesn't support custom logo or no custom logo is set (or it is removed), display default/fallback logo.

So far, this is best code I have to work with:

<?php if ( function_exists( 'the_custom_logo' ) ) : ?>
    <?php if ( 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; ?>
<?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; ?>

Is there a cleaner or more efficient way of doing this without repeating the code for the fallback image twice?

Troy Templeman
  • 275
  • 1
  • 14

2 Answers2

3

Thanks but I think I found a better solution:

<?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; ?>
Troy Templeman
  • 275
  • 1
  • 14
1

You can test both function_exist() and has_custom_logo() in same if condition for getting a single else condition.

$logo = ( ( function_exists( 'the_custom_logo' ) ) && ( has_custom_logo() ) ) ? the_custom_logo() : null;
if ($logo) {
    echo $logo;
} else {
    echo '<h1 class="site-title"><a href="' . esc_url( home_url( '/' ) ) . '" rel="home" title="' . bloginfo( 'name' ) . '"><img src="' . get_stylesheet_directory_uri() . '/images/logo.png" alt="' . bloginfo( 'name' ) . '" width="100" height="50" /></a></h1>';
}
Antoine Subit
  • 9,803
  • 4
  • 36
  • 52