Created
October 23, 2018 04:12
-
-
Save fitodac/0b387ed9a3a3bb440ea92e033fc7af8f to your computer and use it in GitHub Desktop.
WordPress: Post thumbnail
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
/*-----------------------------------------------------------------------*/ | |
/* Display different types of post thumbnails | |
/* USE: | |
/* Include this code in your functions.php file | |
/* Include this function inside the loop: nitro_post_thumbnail() | |
/* Options: | |
/* nitro_post_thumbnail(true) - include links to post for images | |
/*-----------------------------------------------------------------------*/ | |
if( !function_exists( 'nitro_post_thumbnail' ) ): | |
function nitro_post_thumbnail( $link = false ){ | |
if( post_password_required() || is_attachment() ) return; | |
global $post; | |
// Get video thumbnail if is video post format | |
$content = apply_filters( 'the_content', $post->post_content ); | |
$embeds = wp_extract_urls( $post->post_content ); | |
$video_thumbnail = false; | |
// ICONS | |
$icon = ''; | |
if( 'video' == get_post_format() ) $icon = 'play-circle'; | |
if( 'quote' == get_post_format() ) $icon = 'quote-left'; | |
if( 'audio' == get_post_format() ) $icon = 'headphones'; | |
foreach( $embeds as $embed ){ | |
if( strpos($embed, 'youtube') || strpos($embed, 'vimeo') ){ | |
if( strpos($embed, 'vimeo') ){ | |
$id = substr(parse_url($embed, PHP_URL_PATH), 1); | |
$data = file_get_contents("http://vimeo.com/api/v2/video/$id.json"); | |
$data = json_decode($data); | |
$video_thumbnail = $data[0]->thumbnail_large; | |
} | |
if( strpos($embed, 'youtube') ){ | |
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $embed, $match); | |
$id = $match[1]; | |
$video_thumbnail = "https://img.youtube.com/vi/$id/maxresdefault.jpg"; | |
} | |
}else{ | |
$video_thumbnail = $embed; | |
$video_html = true; | |
} | |
} | |
// Set $link as true for display an image link | |
if( !$link ): | |
if( has_post_thumbnail() ): | |
echo '<figure class="post-thumbnail">'; | |
the_post_thumbnail( 'post-thumbnail', array( | |
'alt' => the_title_attribute( array( | |
'echo' => false, | |
) ), | |
) ); | |
echo '</figure>'; | |
endif; | |
else : ?> | |
<?php if( has_post_thumbnail() || get_post_format() ): ?> | |
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true"> | |
<?php if( has_post_thumbnail() ): | |
the_post_thumbnail( 'post-thumbnail', array( | |
'alt' => the_title_attribute( array( | |
'echo' => false, | |
) ), | |
) ); | |
else: | |
if( $video_thumbnail ): | |
if( $video_html ): | |
echo do_shortcode('[video src="'.$video_thumbnail.'"]'); | |
else: ?> | |
<img src="<?php echo esc_attr($video_thumbnail); ?>"> | |
<?php endif; | |
endif; | |
endif; ?> | |
<?php if(get_post_format()): ?><span class="fa fa-<?php echo $icon; ?>"></span><?php endif; ?> | |
</a> | |
<?php endif; ?> | |
<?php endif; | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment