Add a Title Tag to your Logo
This is a Developer Level doc.
If you're unfamiliar with PHP and/or editing files, codes and templates, as well as with resolving possible conflict, please seek help from a professional. Under our Support Policy, we don't provide support for modifications and customization.
/**
* Add a title tag to the logo
*/
function my_custom_logo() {
// The logo
$custom_logo_id = get_theme_mod( 'custom_logo' );
// If has logo
if ( $custom_logo_id ) {
// Attr
$custom_logo_attr = array(
'class' => 'custom-logo',
'itemprop' => 'logo',
);
// Image alt
$image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
if ( empty( $image_alt ) ) {
$custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
}
// Get the image
$html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url" title="%2$s">%3$s</a>',
esc_url( home_url( '/' ) ),
esc_html( get_bloginfo( 'name' ) ),
wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr )
);
}
// If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
elseif ( is_customize_preview() ) {
$html = sprintf( '<a href="%1$s" class="custom-logo-link" style="display:none;"><img class="custom-logo"/></a>',
esc_url( home_url( '/' ) )
);
}
// Return
return $html;
}
add_filter( 'get_custom_logo', 'my_custom_logo' );
All PHP snippets should be added via a child theme's functions.php file.