Created
September 5, 2020 14:26
-
-
Save brankoconjic/c20f35e5819b8632de0090257b8c3b4b to your computer and use it in GitHub Desktop.
Highend Theme - Featured image with custom CAPTION 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
/** | |
* HTML Markup for featured image thumb. | |
* | |
* @since 3.6.4 | |
*/ | |
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'] ); | |
// Caption | |
$caption = ''; | |
if ( is_single( $post_id ) || is_page( $post_id ) ) { | |
$caption = wp_get_attachment_caption( $thumb ); | |
if ( ! empty( $caption ) ) { | |
$caption = '<em class="post-thumb-caption">' . wp_kses( $caption, wp_kses_allowed_html('html') ) . '</em>'; | |
} | |
} | |
// 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> | |
<?php echo $caption; ?> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment