Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Default Image if Custom Field Empty WordPress

  • SOLVED

I have a custom field which is used to hold an image. The field outputs find when displayed as:

<img src="<?php global $post; echo get_post_meta($post->ID, 'custom_image_field', true); ?>" alt="Visit Product" title="Visit Product"/>

I am now trying to create a function so that if the field is left blank, a default image is displayed instead.

I have the following code so far, but just can't get it working:

function default_image() {
$image = get_post_meta($post->ID, 'custom_image_field', true);
if(is_singular( 'reviews' ) && empty( $image ) ) { ?>
<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>
<? }
else if(is_singular( 'reviews' ) && !empty( $image ) ) { ?>
<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>
<? }
}
add_action('thesis_hook_before_sidebars', 'default_image', 1);

Answers (6)

2012-01-19

Julio Potier answers:

Hello just reverse the "!" and add "$post" to global declaration.
function default_image() {
global $post;
$image = get_post_meta($post->ID, 'custom_image_field', true);
if(is_singular( 'reviews' ) && !empty( $image ) ) { ?>
<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>
<? }
else if(is_singular( 'reviews' ) && empty( $image ) ) { ?>
<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>
<? }
}
add_action('thesis_hook_before_sidebars', 'default_image', 1);


robster comments:

Thanks - just needed the global declaration and the new code now looks like Christianto posted below.


Julio Potier comments:

edit :
!empty( $image )
instead of
empty( !$image )
misstyped ...

2012-01-19

Christianto answers:

add Global $post

function default_image() {
global $post;
$image = get_post_meta($post->ID, 'custom_image_field', true);

if(is_singular( 'reviews' ) && empty( $image ) ) { ?>

<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>

<? }

else if(is_singular( 'reviews' ) && !empty( $image ) ) { ?>

<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>

<? }

}

add_action('thesis_hook_before_sidebars', 'default_image', 1);


robster comments:

Thanks - works brilliantly!


Christianto comments:

I'm glad to help,

its not only declare global but also turned around "!" before empty() like every one said..
I think you should split the prize when vote...

btw, from the hook it look like it run on sidebar?

if you have some new query after your content this could be a problem.
I don't know if the hook is running on post init or widget/sidebar call but just in case it run on widget/sidebar called the you should save var $post of main query into new variable and declare it inside the function..

Hope this help..



2012-01-19

Daniele Raimondi answers:

Try to invert the condition. You are using the default if the custom field is not empty.


robster comments:

Well spotted - thanks Daniele!

2012-01-19

Jens Filipsson answers:

Haven't you turned it around? Shouldn't it be like this:

function default_image() {

$image = get_post_meta($post->ID, 'custom_image_field', true);

if(is_singular( 'reviews' ) && !empty( $image ) ) { ?>

<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>

<? }

else if(is_singular( 'reviews' ) && empty( $image ) ) { ?>

<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>

<? }

}

add_action('thesis_hook_before_sidebars', 'default_image', 1);

2012-01-19

Arnav Joy answers:

try this

$image = get_post_meta($post->ID, 'custom_image_field', true);

if(!empty( $image ) ) { ?>

<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>

<? }

else { ?>

<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>

<? }


Arnav Joy comments:

try this

function default_image() {

$image = get_post_meta($post->ID, 'custom_image_field', true);

if(is_singular( 'reviews' ) && !empty( $image ) ) { ?>

<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>

<? }

else { ?>

<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>

<? }

}

add_action('thesis_hook_before_sidebars', 'default_image', 1);

2012-01-19

Gabriel Reguly answers:

Hi robster,

Please try this code:


function default_image() {
global $post;
if ( is_singular( 'reviews' ) {
$image = get_post_meta($post->ID, 'custom_image_field', true);
if ( $image ) ) {
?>
<a href="http://domain.com" title="Custom Field><img src="<?php echo $image; ?>" alt="Custom Field"/></a>
<?php
} else { // empty( $image )
?>
<a href="http://domain.com" title="Default Image"><img src="http://domain.com/default-image.jpg" alt="Default Image" title="Default Image"/></a>
<?php
}
}
}
add_action('thesis_hook_before_sidebars', 'default_image', 1);



Regards,
Gabriel