Question

Am trying to display site logo in my website, which i have uploaded under site-logo section comes under theme->customize->logo->publish

I have added following code in functions.php for support

function theme_prefix_setup() {

    add_theme_support( 'custom-logo', array(
        'height'      => 100,
        'width'       => 400,
        'flex-width' => true,
    ) );

}
add_action( 'after_setup_theme', 'theme_prefix_setup' );

Following code in header.php to display logo

<a class="footer_link" href="index.php"><img src="<?php echo the_custom_logo(); ?>/assets/images/logo.png" /></a>

But logo is not displaying ,please help me

Was it helpful?

Solution

the_custom_logo() displays a custom logo image, linked to home, so you don't need to echo it, wrap it inside a tag or use it as a src for an image.

If you want to use the default output, then just use in your theme file. If there's no custom logo set, then the function results in an empty string i.e. nothing is displayed.

For custom output, first get the custom logo image ID with get_theme_mod( 'custom_logo' ) and then get the image markup with wp_get_attachment_image(). Or the logo image url with wp_get_attachment_url() or wp_get_attachment_image_src.

Example with fallback, modified from how get_custom_logo() works.

$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id ) {
  $custom_logo_attr = array(
    'class' => 'custom-logo',
  );
  printf(
    '<a href="%1$s" class="custom-logo-link" rel="home">%2$s</a>',
    esc_url( home_url( '/' ) ),
    wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr );
  );
} else {
  $custom_logo_attr = array(
    'class' => 'fallback-logo',
  );
  printf(
    '<a href="%1$s" class="custom-logo-link" rel="home">%2$s</a>',
    esc_url( home_url( '/' ) ),
    get_template_directory_uri() . '/assets/images/logo.png'
  );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top