Dalam membuat tema/template WordPress, tentunya di bagian atas atau header website kita, kita ingin menampilkan logo situs jika ada, jika tidak maka kita ingin menampilkan teks judul situs.
Untuk itu, kita perlu script php seperti ini:
$custom_logo_id = get_theme_mod( 'custom_logo' );
$logo = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( has_custom_logo() ) {
echo '<a href="' .get_site_url(). '"><img src="' . $logo[0] . '" alt="' . get_bloginfo( 'name' ) . '" height="60px" style="margin-top: 10px;"></a>';
} else {
echo '<h1><a href="' .get_site_url(). '">'. get_bloginfo( 'name' ) .'</a></h1>';
}
Script ini hendaknya diletakkan di file kepala/header tema WordPress kita.
Oh ya, jangan lupa, pada fungtions.php theme kita perlu ada setingan custom logo seperti ini ya:
//Custom logo
function themename_custom_logo_setup() {
$defaults = array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
'unlink-homepage-logo' => true,
);
add_theme_support( 'custom-logo', $defaults );
}
add_action( 'after_setup_theme', 'themename_custom_logo_setup' );
Pastikan themename di script terakhir ini diganti dengan teks domain theme atau plugin yang kamu buat.