Logo Kustom WordPress dengan Gambar Default/Fallback

Saya mencoba menggunakan fitur logo kustom baru WordPress untuk mencapai hal berikut:

  • Menampilkan logo default/fallback.
  • Jika versi WordPress mendukung logo kustom, izinkan pengguna untuk mengganti logo default/fallback dengan logo kustom di Customizer.
  • Jika versi WordPress tidak mendukung logo kustom atau tidak ada logo kustom yang disetel (atau dihapus), tampilkan logo default/fallback.

Sejauh ini, ini adalah kode terbaik yang harus saya kerjakan:

<?php if ( function_exists( 'the_custom_logo' ) ) : ?>
    <?php if ( has_custom_logo() ) : ?>
        <?php the_custom_logo(); ?>
    <?php else : ?> 
        <h1 class="site-title"><a href="/id<?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="/id<?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; ?>

Apakah ada cara yang lebih bersih atau efisien untuk melakukan ini tanpa mengulangi kode untuk gambar cadangan dua kali?


person Troy Templeman    schedule 16.11.2016    source sumber


Jawaban (2)


Terima kasih, tetapi saya rasa saya menemukan solusi yang lebih baik:

<?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) : ?>
    <?php the_custom_logo(); ?>
<?php else : ?> 
    <h1 class="site-title"><a href="/id<?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; ?>
person Troy Templeman    schedule 16.11.2016

Anda dapat menguji function_exist() dan has_custom_logo() dalam kondisi if yang sama untuk mendapatkan satu kondisi else.

$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="/id' . 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>';
}
person Antoine Subit    schedule 16.11.2016