Allow theme devs to change attrs (like CSS class) of thumbnail images. props scribu. see #10928

git-svn-id: http://svn.automattic.com/wordpress/trunk@12035 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2009-10-15 12:31:48 +00:00
parent 398bfcd57d
commit 0dc4500e09
2 changed files with 8 additions and 7 deletions

View File

@ -527,7 +527,7 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon =
* @param bool $icon Optional, default is false. Whether it is an icon. * @param bool $icon Optional, default is false. Whether it is an icon.
* @return string HTML img element or empty string on failure. * @return string HTML img element or empty string on failure.
*/ */
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) { function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
$html = ''; $html = '';
$image = wp_get_attachment_image_src($attachment_id, $size, $icon); $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
@ -537,12 +537,13 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa
if ( is_array($size) ) if ( is_array($size) )
$size = join('x', $size); $size = join('x', $size);
$attachment =& get_post($attachment_id); $attachment =& get_post($attachment_id);
$attr = array( $default_attr = array(
'src' => $src, 'src' => $src,
'class' => "attachment-$size", 'class' => "attachment-$size",
'alt' => trim(strip_tags( $attachment->post_excerpt )), 'alt' => trim(strip_tags( $attachment->post_excerpt )),
'title' => trim(strip_tags( $attachment->post_title )), 'title' => trim(strip_tags( $attachment->post_title )),
); );
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
$attr = array_map( 'esc_attr', $attr ); $attr = array_map( 'esc_attr', $attr );
$html = rtrim("<img $hwstring"); $html = rtrim("<img $hwstring");

View File

@ -909,18 +909,18 @@ function get_post_image_id( $post_id = NULL ) {
return get_post_meta( $post_id, '_thumbnail_id', true ); return get_post_meta( $post_id, '_thumbnail_id', true );
} }
function the_post_image( $size = 'thumbnail' ) { function the_post_image( $size = 'thumbnail', $attr = '' ) {
echo get_the_post_image( $size ); echo get_the_post_image( NULL, $size, $attr );
} }
function get_the_post_image( $size = 'thumbnail', $post_id = NULL ) { function get_the_post_image( $post_id = NULL, $size = 'thumbnail', $attr = '' ) {
global $id; global $id;
$post_id = ( NULL === $post_id ) ? $id : $post_id; $post_id = ( NULL === $post_id ) ? $id : $post_id;
$post_image_id = get_post_image_id( $post_id ); $post_image_id = get_post_image_id( $post_id );
$size = apply_filters( 'post_image_size', $size ); $size = apply_filters( 'post_image_size', $size );
if ( $post_image_id ) { if ( $post_image_id ) {
do_action( 'begin_fetch_post_image_html', $post_id, $post_image_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters do_action( 'begin_fetch_post_image_html', $post_id, $post_image_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
$html = wp_get_attachment_image( $post_image_id, $size ); $html = wp_get_attachment_image( $post_image_id, $size, false, $attr );
do_action( 'end_fetch_post_image_html', $post_id, $post_image_id, $size ); do_action( 'end_fetch_post_image_html', $post_id, $post_image_id, $size );
} else { } else {
$html = ''; $html = '';