WordPress Pods 2.0 Framework - rendering featured image in template
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 | |
if (!function_exists('get_post_featured_image')) { | |
/** | |
* Finds featured image for the post specified by $post_id and returns all the properties for the desired $size | |
* @param int $post_id [id of the post whose featured image we would like to view] | |
* @param string/array $size [size of the image, parameter is identical to the one specified for <code>wp_get_attachmment_image_src</code> <http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src> ] | |
* @return array [returns an array with following key value pairs: id, url, alt, caption, description] | |
*/ | |
function get_post_featured_image($post_id, $size) { | |
$return_array = []; | |
$image_id = get_post_thumbnail_id($post_id); | |
$image = wp_get_attachment_image_src($image_id, $size); | |
$image_url = $image[0]; | |
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); | |
$image_post = get_post($image_id); | |
$image_caption = $image_post->post_excerpt; | |
$image_description = $image_post->post_content; | |
$return_array['id'] = $image_id; | |
$return_array['url'] = $image_url; | |
$return_array['alt'] = $image_alt; | |
$return_array['caption'] = $image_caption; | |
$return_array['description'] = $image_description; | |
return $return_array; | |
} | |
} | |
//for one item | |
$row = $obj->row(); | |
$post_id = $row['ID']; | |
$image_properties = get_post_featured_image($post_id, 'full'); | |
//display image | |
print('<img src="'.$image_properties[url].'" alt="'.$image_properties['alt'].'" />'); | |
// display caption if present | |
if($image_properties['caption'] != null) { | |
print('<p class="wp-caption-text">'.$image_properties['caption'].'</p>'); | |
} | |
// display description if present | |
if($image_properties['description'] != null) { | |
print('<p class="wp-description-text">'.$image_properties['description'].'</p>'); | |
} | |
// for multiple items | |
$all_rows = $obj->data(); | |
foreach( $all_rows as $row ) { | |
$post_id = $row->ID; | |
$image_properties = get_post_featured_image($post_id, 'thumbnail'); | |
//display image | |
print('<img src="'.$image_properties[url].'" alt="'.$image_properties['alt'].'" />'); | |
// display caption if present | |
if($image_properties['caption'] != null) { | |
print('<p class="wp-caption-text">'.$image_properties['caption'].'</p>'); | |
} | |
// display description if present | |
if($image_properties['description'] != null) { | |
print('<p class="wp-description-text">'.$image_properties['description'].'</p>'); | |
} | |
} |
Thanks for your work, I think it would be beneficial.
Where should the file be placed?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi makbeta,
You mentioned on this Wordpress forum post that you had created a function for Wordpress Social Login to integrate with Buddypress avatars: http://wordpress.org/support/topic/does-this-work-with-existing-registered-buddypress-users?replies=25#post-4146906.
Would you mind sharing this function with me? In desperate need of this code for a project Buddypress website going live in a few days. Please feel free to e-mail me at youmatter@yay.do
Thank you --