I am trying to make a widget which takes post id as input from user and displays images of the specified post in sidebar. wp_get_attachment_url()
gets me the url but in the next line wp_get_attachment_image()
doesn't display image on same photo=>id. I don't know where I am going wrong.
I am confirming urls from the post content because otherwise wp_get_attachment_image()
was returning all of the media libary. But now wp_get_attachment_image()
no longer working.
Sharing the code of the widget I am tried to put together after going through other wordpress support group and stack exchange posts:
function widget( $args, $instance ) {
echo $args['before_widget'];
$photos = get_children( array('post_parent' => $instance['post'], 'post_type' => 'attachment', 'post_mime_type' => 'image') );
//for getting inline post images only
$post_id=$instance['post'];
$content_post = get_post($post_id);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
if (!empty($photos)) :
//Loop through each attachment..
foreach ($photos as $photo_id => $photo) :
//getting inline post images only
$url = wp_get_attachment_url($photo_id);
$position = strpos($content, $url);
if($position){
echo $post_id;
//working shows all the attached photo urls
echo wp_get_attachment_url($photo_id);
//not working
wp_get_attachment_image($photo_id, 'thumbnail') ;
}
endforeach ;
endif ;