Last active
September 5, 2020 11:09
-
-
Save brankoconjic/f95e71973ad147a15a2e67db9d526170 to your computer and use it in GitHub Desktop.
Highend Theme - Featured image with custom ALT attribute.
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
function highend_featured_image_thumb( $args = array() ) { | |
$args = wp_parse_args( | |
$args, | |
array( | |
'post_id' => '', | |
'width' => false, | |
'height' => false, | |
'crop' => false, | |
'alt' => '', | |
'lightbox' => false, | |
) | |
); | |
$post_id = $args['post_id']; | |
$thumb = get_post_thumbnail_id( $post_id ); | |
$image = highend_resize( $thumb, $args['width'], $args['height'], $args['crop'] ); | |
// If custom ALT tag is provided use it, otherwise use page title. | |
if ( empty( $args['alt'] ) ) { | |
$thumb_alt = trim( strip_tags( get_post_meta( $thumb, '_wp_attachment_image_alt', true ) ) ); // phpcs:ignore | |
if ( ! empty( $thumb_alt ) ) { | |
$args['alt'] = $thumb_alt; | |
} else { | |
$args['alt'] = get_the_title(); | |
} | |
} | |
if ( empty( $image ) || ! isset( $image['url'] ) || empty( $image['url'] ) ) { | |
return; | |
} | |
if ( $args['lightbox'] ) { | |
$atts = 'data-title="' . esc_attr( get_the_title( $post_id ) ) . '" href="' . esc_url( wp_get_attachment_url( $thumb ) ) . '" rel="prettyPhoto"'; | |
} else { | |
$atts = 'href="' . esc_url( get_permalink( $post_id ) ) . '"'; | |
} | |
$srcset = $args['crop'] ? '' : ' srcset="' . esc_attr( wp_get_attachment_image_srcset( $thumb, array( $image['width'], $image['height'] ) ) ) . '"'; | |
?> | |
<div class="featured-image item-has-overlay"> | |
<a <?php echo $atts; ?>> | |
<img src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $args['alt'] ); ?>" width="<?php echo esc_attr( $image['width'] ); ?>" height="<?php echo esc_attr( $image['height'] ); ?>"<?php echo $srcset; ?> /> | |
<div class="item-overlay-text"> | |
<div class="item-overlay-text-wrap"> | |
<span class="plus-sign"></span> | |
</div> | |
</div> | |
</a> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment