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 badges1 Answer
Reset to default 0I 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
版权声明:本文标题:customization - Get Custom Logo Alt Text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285783a1927531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论