0

I'm looking to pull all of the metadata (e.g. alt, width, height) from the theme logo, which is uploaded through Appearance -> Customization into my theme template file. This is what I currently have, but it's not working:

$custom_logo_id = get_theme_mod('custom_logo');
if ($custom_logo_id) {
    $image = wp_get_attachment_image_src($custom_logo_id, 'full');
    $meta = wp_get_attachment_metadata($custom_logo_id);
    echo '<img src="' . $image[0] . '" alt="' . $meta['alt'] . ' width="' . $meta['width'] . '" height="' . $meta['height'] . '">';
} else {
    echo bloginfo('name');
}
7
  • That looks like it should work. What is the problem with the output you get?
    – cjbj
    Apr 4, 2017 at 15:06
  • I get the width and the height but it's not pulling the alt metadata: Notice: Undefined index: alt in C:\xampp\htdocs\vistex-foundation\wp-content\themes\brandozz\header.php on line 28 width=
    – brandozz
    Apr 4, 2017 at 16:12
  • Perhaps because no al-text was defined when the image was uploaded?
    – cjbj
    Apr 4, 2017 at 16:15
  • No, I checked that...it is defined
    – brandozz
    Apr 4, 2017 at 16:16
  • Ah, wait I remember something about this. I'll get back to you shortly.
    – cjbj
    Apr 4, 2017 at 16:19

2 Answers 2

1

Ends up that "alt" is not stored in wp_get_attachment_metadata so I pulled it from the post meta:

$custom_logo_id = get_theme_mod('custom_logo');
if ($custom_logo_id) {
    $image = wp_get_attachment_image_src($custom_logo_id, 'full');
    $meta = wp_get_attachment_metadata($custom_logo_id);
    **$alt_text = get_post_meta($custom_logo_id, '_wp_attachment_image_alt', true);**
    echo '<img src="' . $image[0] . '" alt="' . $alt_text . '" width="' . $meta['width'] . '" height="' . $meta['height'] . '">';
} else {
    echo bloginfo('name');
}
1
  • I added the $alt_text variable which pulls it from get_post_meta
    – brandozz
    Apr 4, 2017 at 16:23
0

As you can see from the description of wp_get_attachment_metadata there's no alt field stored in that location. The easiest way out would be to just use the title-field for the alt.

Otherwise you can retrieve the alt-field using get_post_meta. More explanation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.