admin管理员组

文章数量:1122832

I'm trying to get the alt text of the custom logo specified in the theme customization. Unfortunately this doesn't work because it seems the alt text is not included in the post meta:

$custom_logo_id = get_theme_mod( 'custom_logo' );
get_post_meta($custom_logo_id, '_wp_attachment_image_alt', true);

I would have used get_custom_logo which includes the alt text, but I'm building my own custom srcset and sizes attributes, and need ONLY the alt text to add to it.

What other options do I have?

I'm trying to get the alt text of the custom logo specified in the theme customization. Unfortunately this doesn't work because it seems the alt text is not included in the post meta:

$custom_logo_id = get_theme_mod( 'custom_logo' );
get_post_meta($custom_logo_id, '_wp_attachment_image_alt', true);

I would have used get_custom_logo which includes the alt text, but I'm building my own custom srcset and sizes attributes, and need ONLY the alt text to add to it.

What other options do I have?

Share Improve this question asked Dec 20, 2019 at 0:01 geochantogeochanto 13115 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I know this question is pretty old, but I needed this today. What I ended up doing was running the get_custom_logo_image_attributes filter, storing the array provided within it when it gets run, and accessing that afterward.

In functions.php:

/* custom logo attribute collection */
add_filter( 'get_custom_logo_image_attributes', function($custom_logo_attr, $custom_logo_id, $blog_id ) {
    global $logo_attrs;
    $logo_attrs = $custom_logo_attr;
    return $custom_logo_attr;
}, 10, 3);

function get_custom_logo_image_attributes() {
    global $logo_attrs;
    return $logo_attrs;
}

Then when it's time to get the data, you have to first be sure the filter has run:

get_custom_logo(); // ensure logo filter is called before our function
$logo_attrs = get_custom_logo_image_attributes();

Once you have that, you can use $logo_attrs['alt'] where you need it.

It's not pretty, but it got the job done.

本文标签: customizationGet Custom Logo Alt Text