Support » Fixing WordPress » Show specific photo in sidebar, depending on the post being viewed

  • Using a php logic Widget for sidebars.. i would like to..

    Display a widget, only on posts… which contain a url to an image..

    so if post 1 has custom field “pic_field” value=”http://…..cool.jpg/”
    and post 2 has custom field “pic_field” value=”http://…….awesome.jpg/”

    Then…. when someone is viewing post 1.. the sidebar will show cool.jpg

    and if someone is viewing post 2, then they will see awesome.jpg in the side bar… but only while viewing that posts individual page..

    i know how to make the custom field, i mean thats a no brainer i just add it on the post..

    what would i put in a php logic widget in order to display these images, Only on a posts page, only of that post contains the url to an image, inside of that particular custom field value..

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you are using the Widget Logic plugin, then you can use code like this:

    is_single(1742) && ($meta = get_post_meta(1742,'pic_field',true)) && preg_match('/^http:\/\/.*jpg/',$meta)

    Replace the 1742 with the ID of the post you want to match.

    Then use the img tag in the widget to display the proper image.

    Thanks vtxyzzy, the thing is I dont want to target a post.

    I want the code in the widget, to detect if that particular post being viewed, has this custom field, and if yes, to displays its specific value

    Well, the logic is similar. I have not tested this, but it should be close:

    global $post;
    is_single() &&
          ($meta = get_post_meta($post->ID,'pic_field',true) ) &&
          preg_match('/^http:\/\/.*jpg/',$meta) ) {
       echo "<img src='$meta' alt='Image for post $post->ID' />";
    }

    cool, not knowing much id say this is likely what im looking for, itll be a bit before i can test it myself.

    THank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show specific photo in sidebar, depending on the post being viewed’ is closed to new replies.