Alt and title attribute
-
Hi,
I’d like to have alt and title attributes with the thumbnails. How can I get them?
Thanks
-
Any update on this?
Hi @okoth1, sorry for the delay! It’ll sound trite but I was under the weather etc etc.
When you’re using YARPP’s “Thumbnails” theme, “title” attributes are automatically added to the<a>
tag that wraps the<img>
tag, and it’s taken from the post’s name.
The “alt” tag is added to the<img>
tag, and it’s taken from the media’s “Alternative Text” field.To make it clearer, here’s the HTML generated from YARPP using the Thumbnails theme for one of the related entries:
<a class="yarpp-thumbnail" rel="norewrite" href="https://yarpp.test/2020/07/17/at-lacinia-massa/" title="at lacinia massa"> <img src="https://yarpp.test/wp-content/uploads/2020/07/worksofast-120x120.gif" class="attachment-yarpp-thumbnail size-yarpp-thumbnail wp-post-image" alt="HEre is the alternative text, thank you." loading="lazy" data-pin-nopin="true" srcset="https://yarpp.test/wp-content/uploads/2020/07/worksofast-120x120.gif 120w, https://yarpp.test/wp-content/uploads/2020/07/worksofast-150x150.gif 150w" sizes="(max-width: 120px) 100vw, 120px" width="120" height="120"> <span class="yarpp-thumbnail-title">at lacinia massa</span> </a>
So if you’re setting the “Alternative Text” on your media when it’s uploaded, and you’re happy with the “title” coming from the post’s name, then you shouldn’t need to do anything differently.
Does that answer your question? Or is there something else you were wanting?
-
This reply was modified 4 years, 6 months ago by
Michael Nelson.
Yes, no problem Michael, it’s just that I saw other questions being answered. I hope you’re OK.
All of my images have alt, title and caption fields filled in. All images are added/used as Featured images on single posts and I have the feeling it has something to do with the Featured images.
But even when I add attributes manually they won’t show up.
$post_thumbnail_html = get_the_post_thumbnail(null, $dimensions['size'], array('data-pin-nopin' => 'true', 'title' => 'WontShowTitle', 'alt' => 'WontShowAlt', 'class' => 'classNoProblem') );
It would be great if you know a solution for this.
Thanks
Hmmm, could you provide a URL of a page where you’re having this issue? And where is that code being placed?
Hi Michael,
This page has the issue
https://www.dapurkobe.co.id/burger-tempe-crispybut all other single recipe page has it.
The code I put in plugins/yet-another-related-posts-plugin/includes/template_thumbnails.php around line 38
I’m getting the alt and title names of featured images on other places this way
$alt_name = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); $title_name = get_the_title($image_id);
If you could echo the values into
$post_thumbnail_html = get_the_post_thumbnail(null, $dimensions['size'], array('data-pin-nopin' => 'true', 'title' => 'title_name', 'alt' => 'alt_name' , 'class' => 'optional_class_name') );
it might be solved.
https://wordpress.stackexchange.com/questions/193196/how-to-get-image-title-alt-attribute/330599
-
This reply was modified 4 years, 6 months ago by
Okoth1.
Hmmm I see the
<a>
actually *does* at least have a title attribute, but yes I see the alt attribute is missing.Just double-checking: I made a video showing how I’m adding the
alt
tags onto images, in case we’re doing something different: https://drive.google.com/file/d/1eqf7BJfXW98IkZvqWYKpwHt0YwrSXkcE/view?usp=drivesdkBut I’d say that if you’re modifying YARPP’s code directly to add the
alt
tags, and they’re still not appearing, I suspect some other plugin, or maybe your theme, is filtering the HTML produced. If you have a test site, I’d suggest deactivating other plugins to find out which. But YARPP by itself doesn’t have this issue (or at least I can’t see how it does.)Do you have an example were you add an image directly as featured post and that image will have an alt text on the website?
I think WordPress removed the alt text for featured posts from version 4.9.
I don’t think one of my plugins cause this.
I’m deactivating plugins and made some progress. You might be right, mister ??
I’m going to find out what’s causing it and let you know.
Thanks
I found the plugin that’s causing it and I’ll notify the creator.
Thank you for pointing me in the right direction.
Phew! Good guess! (Actually it’s a pretty common response: when you don’t understand what’s going wrong: blame another plugin! ??)
Thanks for tracking it down @okoth1.
And that was a very good-looking site. Well done! And take care!Hi Michael,
I fully agree on this “blame another plugin!” And then people expect me to deactivate all plugins, which basically means to take the site offline… ??
This site is from a period before people use their phones to browse online. ?? It’s still working but it’s going to get a mobile friendly design. I’m glad you like it.
-
This reply was modified 4 years, 6 months ago by
- The topic ‘Alt and title attribute’ is closed to new replies.