• Melly

    (@melissaod)


    Hello. I am using our company logo in the header. At the time I uploaded the logo image I input the Alt attribute. When I go into the Media library and check the info for the logo image it has the alt attribute field completed. However, I have used several different SEO analysis tools via several different websites and they ALL show my logo as having no ALT attribute.

    I tried to solve the problem myself by doing some research on the internet. Per my research I added some code to my child theme’s functions.php file but still I get the same error message

    image has no alt attribute:
    
    https://collectiblesandmoreinstore.com/wp-content/uploads/2020/02/collectibles-and-more-in-store-abbreviation-camis-slogan-logo.jpg

    Please can you help me? The code I currently have added is:

    add_theme_support( 'custom-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" rel="home" 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"><img /></a>',
    			esc_url( home_url( '/' ) )
    		);
    	}
    
    	// Return
        return $html; 
    	
    }
    add_filter( 'get_custom_logo', 'my_custom_logo' );
    • This topic was modified 5 years ago by Jan Dembowski. Reason: Formatting

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • garethgillman

    (@garethgillman)

    Your logo has the title attribute set which is the better option for logos, alt attributes describe an image (such as a person or an object). You don’t have to include an alt attribute on every image.

    Most “seo tools” just follow the minimum guidelines, if you want better seo audit from Google, use lighthouse (if you’re using chrome)

    Moderator bcworkz

    (@bcworkz)

    Assigning alt text to $custom_logo_attr['alt'] is the correct way to pass an alt attribute to wp_get_attachment_image(). The problem is if $image_alt is not empty, this never happens. There may be a more elegant solution, but you could just add an else clause to the if( if_empty( $image_alt )){...} conditional.

    else {
      $custom_logo_attr['alt'] = $image_alt;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Logo Alt Atrribute code for functions.php’ is closed to new replies.