Skip to content

Commit

Permalink
Filter the post thumbnail output rather than replace it
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottSmith95 committed Aug 31, 2014
1 parent af69f0c commit 85f582c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 23 deletions.
2 changes: 1 addition & 1 deletion content-excerpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<header class="entry-header">
<?php if ( has_post_thumbnail() && get_theme_mod( 'show_featured_images_on_excerpts', false ) == true ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php decode_srcset_post_thumbnail(); ?>
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<div class="entry-title"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
Expand Down
2 changes: 1 addition & 1 deletion content-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<header class="entry-header">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php decode_srcset_post_thumbnail(); ?>
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<div class="entry-title"><h2><?php decode_print_post_title() ?><?php if ( get_theme_mod( 'link_post_title_arrow', false ) == true ) echo '<span class="link-title-arrow">&#8594;</span>'; ?></h2></div>
Expand Down
4 changes: 2 additions & 2 deletions content-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<header class="entry-header">
<?php if ( has_post_thumbnail() && get_theme_mod( 'show_featured_images_on_singles', false ) == true ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php decode_srcset_post_thumbnail(); ?>
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<div class="entry-title"><h2><?php decode_print_post_title() ?><?php if (get_theme_mod( 'link_post_title_arrow', false ) == true ) echo '<span class="link-title-arrow">&#8594;</span>'; ?></h2></div>
Expand Down Expand Up @@ -99,7 +99,7 @@
<header class="entry-header">
<?php if ( has_post_thumbnail() && get_theme_mod( 'show_featured_images_on_singles', false ) == true ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php decode_srcset_post_thumbnail(); ?>
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<div class="entry-title"><h2><?php the_title(); ?></h2></div>
Expand Down
2 changes: 1 addition & 1 deletion content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<header class="entry-header">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php decode_srcset_post_thumbnail(); ?>
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<div class="entry-title"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
Expand Down
23 changes: 5 additions & 18 deletions inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,18 @@
/**
* Display navigation to next/previous set of posts when applicable.
*/
function decode_srcset_post_thumbnail() {
$post = get_post();
$post_id = $post->ID;

function decode_srcset_post_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' );

$attr['srcset'] = $large_image_url[0] . ' 1x, ' . $full_image_url[0] . ' 2x';
$attr['width'] = '100%';
$attr['height'] = '100%';
$attr = 'srcset=' . $large_image_url[0] . ' 1x, ' . $full_image_url[0] . ' 2x';

echo get_the_post_thumbnail( $post_id, 'full', $attr );
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

/* Older version that outputs everything itself, I'd rather use built-in WP functions.
echo '<img
width="' . $full_image_url[1] . '"
height="' . $full_image_url[2] .'"
srcset="' . $large_image_url[0] . ' 1x, ' . $full_image_url[0] . ' 2x"
src="' . $large_image_url[0] . '"
class="attachment-post-thumbnail wp-post-image"
alt="' . get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true ) . '"
>'
*/
return $html;
}
endif;
add_filter( 'post_thumbnail_html', 'decode_srcset_post_thumbnail', 10, 5 );

if ( ! function_exists( 'decode_paging_nav' ) ) :
/**
Expand Down

0 comments on commit 85f582c

Please sign in to comment.