Show specific photo in sidebar, depending on the post being viewed (5 posts)

  1. miketopher
    Posted 3 years ago #

    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

  2. vtxyzzy
    Posted 3 years ago #

    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.

  3. miketopher
    Posted 3 years ago #

    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

  4. vtxyzzy
    Posted 3 years ago #

    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' />";
  5. miketopher
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic