About Sridhar Katakam
I am an independent WordPress web consultant with 10 years of experience in WordPress theme installation, customization, administration, maintenance, support, documentation, troubleshooting and PSD/design to WP.
Genesis and WordPress Tutorials
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.
Thanks a lot Sridhar, I was searching for this since a long time! 🙂
For accessibility, shouldn’t the alt text be more specific like, “waves crashing onto sandy beach”?
I think so, yes.
From a SEO perspective I think it’s better to have the alt text be same as post titles.
I am not knowledgeable about SEO and Accessibility to authoritatively say which is better, custom descriptive Alt text or post titles.
The tutorial is merely about how to do a certain task, not necessarily that one should be doing it 🙂
Yeah, I get that. And what you’ve done is much more useful than having default file names or no alt text!
It should be possible to set the post titles as alt text only if the image has it empty.
Hi Sridhar,
I had this code running since you give it to us and it was working great but following update to the last version of genesis, it doesn’t work anymore. Do you have any idea what has been changed and how to have the code running again with the last version of genesis?
Thanks a lot!
I just tested the latest version of Genesis and this code snippet is not needed any more.
The featured images get the post titles as alt text out of the box.
Thanks Sridhar.
I’ve just tested and not on my website, the Alt & Title are the name of the image, not the title of the post…and I don’t see any option for this in genesis option panel.
or maybe the featured image but not the other images on the post, and i’m not displaying the featured image on my posts…only the images in the content of the post. Is there a way to modify the alt & title attribute for all the image in a post?
This tutorial is about automatically setting the alt text of featured images that appear on the content archives per Genesis theme settings to that of the Post titles.
What you are looking for namely, setting alt text of images present inside the content is not something that is commonly done as far as I know. I do not know of a easy/straight forward way of getting this done. If you know how to do it in WordPress please share the solution so that I can see how it can be adapted (if need be) in Genesis.
I’ve managed to do it with the ALT attribute with this function:
/* Register callback function for post_thumbnail_html filter hook */
add_filter( ‘the_content’, ‘post_image_alt_change’, 10, 5 );
/* Function which will replace alt atribute to post title */
function post_image_alt_change( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$post_title = get_the_title();
$post_category = get_the_category();
$postcategories = get_the_category();
$post_category=”;
if ($postcategories) {
foreach($postcategories as $category) {
$post_category .= $category->name .’ ‘;
}
}
$post_alt = $post_title . ‘ ‘ . $post_category;
$html = preg_replace( ‘/(alt=”)(.*?)(“)/i’, ‘$1’.esc_attr( $post_alt ).’$3′, $html );
return $html;
}
I would like to do it for the title as well but could not find how to do it. Any idea?
Thanks!
Can you paste the code at pastebin.com or pastie.org and give the link?
Here is the link: http://pastebin.com/2BKJjz3u
I pasted it in my test Genesis site’s functions.php and see:
http://d.pr/i/1eCZq
Does the code work fine for you?
yes it work fine for me. Is your image a featured image? as on my site I didn’t display featured image inside the post content.
Tested in a fresh Genesis site. Adding the code in functions.php causes the warning to appear. Can you test in another site?
I’ve put the code on two websites and it works on the two websites. Could you try by removing the featured image display from post and only add image in the content?
Hi, the snipplet works fine on archive pages. Is there an easy way to make this work also for the Geneses Featured Post widget?