0

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 ;
3

When saying "not working" you should include what you expect and what you get; "not working" says very little about the problem you have. Without knowing what the exact problem you have, I guess you don't see the image. In that case I suggest to change this:

wp_get_attachment_image($photo_id, 'thumbnail');

With:

echo wp_get_attachment_image($photo_id, 'thumbnail');

wp_get_attachment_image() returns the HTML image element, but doesn't print it.

4
  • Hi. Thank you for pointing out the problem with my question statement and thank you for answering anyways. :) Implemented your suggestion. It worked! :D – Beans Nov 21 '15 at 8:39
  • Please, if it worked mark the answer as accepted so other users can find the correct answer quickly (check the V sign under vote buttons in the answer). – cybmeta Nov 21 '15 at 8:42
  • I have clicked on the upward pointing arrow sign but its not displaying the change. A pop indicates I have to have 15+ reputation for the score to display publicly. Is there any other way to accept the answer? – Beans Nov 21 '15 at 8:48
  • Accepting the answer is not the same that voting it up. There is a grey check mark under the vote arrows, click on that check mark to accept the answer, not on the upward or downward arrow. Yo don't need a specific amount of reputation points to accept an answer. I hope you see the difference. – cybmeta Nov 21 '15 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.