Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It's my first time here so here goes :)

I've created a custom image field which allows me to upload new images, or have images added via a form from the frontend. I want these to display.

I'm creating a shortcode like so:

    function display_ad() {
    global $post;
    return do_shortcode( '<a href="' . get_permalink($id) . '">' . '<img src="' . get_post_meta( $post->ID , "wpcf-display-ad" , true ) . '"/>' . '</a>' );
    }
    add_shortcode('display-ad', 'display_ad');

This works fine - images are displayed and link to their respective post, but for posts that don't contain a display image, the src tag still returns (and any css, for example a border, too).

I'm struggling to figure out how to apply the if statement to the above code, i.e:

    if ( get_post_meta( $post->ID , "wpcf-display-ad" , true ) ) :

... so that if an image doesn't exist then nothing is returned.


Hi Abdussamad

Thanks for your suggestions. I've just spent the last hour trying to wrap my head around them and them to work, but no joy. I must warn you I'm very much at the early stages of PHP :)

Here's what I've done based on your comments:

function display_ad() {
global $post;
$content = get_permalink($id);
$ad_image = get_post_meta( $post->ID , "wpcf-display-ad" , true );
if( empty( get_post_meta( $post->ID, 'wpcf-display-ad', true ) ) ) {
return $content;
} else {
return $content . $ad_image;
}
}
add_shortcode('display-ad', 'display_ad');

However, this just returns a white screen.

I have two posts up at the moment on my development site (one with a display ad, the other not), and if I remove the if statement bit, and just return $content; then the posts and their permalinks display. If I just return $ad_image; again the posts display together, one showing the path to the image, the other blank (but present in Chrome's Developer Tools panel).

However, with the if statement I get the white screen. Evidently I'm doing something wrong. Thanks for your help.

Gavin.

share|improve this question

1 Answer 1

First up the display_ad function should have a parameter $content that contains the post's content upto the point where the shortcode is placed. So you concatenate that $content with your advertisement image before returning it.

Second what's wrong with :

if( empty( get_post_meta( $post->ID, 'wpcf-display-ad', true ) ) ) {
   return $content;
 } else {
    return $content . $ad_image;
 }
share|improve this answer
    
Hi Abdussamad - I've just elaborated in the original post on your suggestions. Thanks :) –  user21165 Oct 6 '12 at 7:34
    
Actually - looking at the code, it looks as though if the post meta doesn't exist, then show the content. However, what I want is for the content not to show at all if the display ad field is empty. –  user21165 Oct 6 '12 at 7:45
    
Okay - I think I've found a solution. To use my original code and filter by taxonomy. That should work. Thanks for your help. –  user21165 Oct 6 '12 at 8:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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