Last active
May 19, 2017 05:28
-
-
Save donnamcmaster/4a52166a359b351c342396144c0bf3bb to your computer and use it in GitHub Desktop.
WordPress: set a title attribute in gallery output; some plugins will display this as a caption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Title Field | |
* - adds image caption to image tags; will show up as gallery captions | |
* - this example uses image caption if defined, with fallback to post title | |
* - edit as desired to choose post title, description, or caption (excerpt) | |
*/ | |
add_filter( 'wp_get_attachment_link', function( $anchor, $id ) { | |
$_post = get_post( $id ); | |
if ( !$_post ) { | |
return $anchor; | |
} | |
$title = $_post->post_excerpt; // caption | |
$title = $title ? $title : $_post->post_title; // actual title | |
// $title_tag = $_post->post_content; // description | |
$title = esc_html( $title ); | |
$anchor = str_replace( 'href', "title='$title' href", $anchor ); | |
return $anchor; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
5/18/17 - added
$title = esc_html( $title );
as it was breaking when the caption/title had quote characters in it