Last active
August 29, 2015 14:16
-
-
Save jrobinsonc/3959a3c40138fdb701c8 to your computer and use it in GitHub Desktop.
Wordpress helper: Get image tag.
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
<?php | |
/** | |
* get_thumb_tag | |
* | |
* @author JoseRobinson.com | |
* @link https://gist.github.com/jrobinsonc/3959a3c40138fdb701c8 | |
* @version 201506211936 | |
* @param int $post_id | |
* @param mixed $size | |
* @param boolean $caption | |
* @return string | |
*/ | |
function get_thumb_tag($post_id, $size, $caption = true) | |
{ | |
$thumbnail_id = get_post_thumbnail_id($post_id); | |
if ('' === $thumbnail_id) | |
return ''; | |
$image_obj = wp_get_attachment_image_src($thumbnail_id, $size); | |
if (false === $image_obj) | |
return ''; | |
$post_info = get_post($thumbnail_id); | |
if (null === $post_info) | |
return ''; | |
// $image_obj[1], $image_obj[2] | |
$result = sprintf('<figure class="post-media-%s">', $thumbnail_id); | |
$result .= sprintf('<img src="%s" alt="%s" />', $image_obj[0], esc_attr($post_info->post_excerpt)); | |
if (true === $caption) | |
{ | |
if ($post_info->post_content !== '') | |
$result .= sprintf('<p>“%s”</p>', $post_info->post_content); | |
if ($post_info->post_excerpt !== '') | |
$result .= sprintf('<figcaption>%s</figcaption>', $post_info->post_excerpt); | |
} | |
$result .= sprintf('</figure>'); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment