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.