Tuesday, 29 October 2013

wordpress shortcode to make featured image gallery from pages

what I was looking to do here is to have a shortcode that shortcode that shows the featured images of a range of Post or Pages.

One issue I've had to overcome here is that I couldn't get get_the_post_thumbnail to work for me.

Here's the code I've written which you'll need to put in your templates function.php
function page_featured_images_ofparent() {
$postid = get_the_ID();
$children = get_children($postid);

foreach ($children as $child):
// print_r($child);
 //echo "

"; $child_id = $child->ID; $URL = $child->guid; $title = $child->post_title; // echo "Child Id = $child_id
"; if(has_post_thumbnail( $child_id )): $image = wp_get_attachment_image_src( get_post_thumbnail_id( $child_id ), 'thumbnail' ); // 'single-post-thumbnail' $image_url = $image[0]; echo ''; endif; endforeach; return; } add_shortcode ('page-feat-img-childs', 'page_featured_images_ofparent');

and you can use the code

Put this wordpress shortcode on any of your posts or pages and it will show all the featured images of any child post or blogs.

No comments: