I am displaying a post gallery inside a custom post type, using the following in my single-custom.php template:
<?php echo do_shortcode('
One of the things included in the output is title="filename"
in the <img>
tag, like so:
<img width="150" height="150" src="http://mydomain.com/wp-content/uploads/DSC_0036-150x150.jpg" class="attachment-thumbnail" alt="DSC_0036" title="DSC_0036" />
I do not want to output the title attribute. Mostly, I don’t want crappy filenames in the hover-over text, but there are hundreds of images and I’m not prepared to go into the Media Gallery and edit the title for each one. I’d prefer to also change the alt
attribute to the post name, perhaps.
Is there a way to filter the gallery to output the post name in the alt and title attributes, instead of the filename?
The
[gallery]
shortcode is parsed bygallery_shortcode()
, which callswp_get_attachment_link()
, which in turns callswp_get_attachment_image()
. This latter function writes the<img>
tag with thetitle
attribute. You’re lucky, because the attributes are filtered throughwp_get_attachment_image_attributes
, so you can hook into that and remove thetitle
.To do this, you attach the hook before you call the shortcode and remove it after you did this. You can either do this in your template if it’s a one-off, or, if you are more advanced, you “hijack” the
[gallery]
shortcode with your own function that adds the hook, calls the originalgallery_shortcode()
function, and removes the hook.This is very useful, thanks to the both of you.
You can also add a copyright title attribute with the year automatically set:
$attr['title'] = '© ' . date("Y") . ' Copyright ' . get_option('blogname');