Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$10
Display Default Image if Custom Field Empty
<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);
This question has been answered.
robster | 01/19/12 at 8:24am
Edit
(6) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
01/19/12
8:52amJulio Potier says: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);Previous versions of this answer: 01/19/12 at 8:29am | 01/19/12 at 8:52am
- 01/19/12 8:49am
robster says:Thanks - just needed the global declaration and the new code now looks like Christianto posted below.
- 01/19/12 8:53am
Julio Potier says:edit :
!empty( $image )
instead of
empty( !$image )
misstyped ...
- 01/19/12 8:49am
-

Last edited:
01/19/12
8:28amChristianto says: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);- 01/19/12 8:50am
robster says:Thanks - works brilliantly!
- 01/19/12 9:08am
Christianto says: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..
- 01/19/12 8:50am
-

Last edited:
01/19/12
8:28amJens Filipsson says: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); -

Last edited:
01/19/12
8:29amDaniele Raimondi says:Try to invert the condition. You are using the default if the custom field is not empty.
- 01/19/12 8:52am
robster says:Well spotted - thanks Daniele!
- 01/19/12 8:52am
-

Last edited:
01/19/12
8:30amArnav Joy says: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>
<? }- 01/19/12 8:37am
Arnav Joy says: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);
- 01/19/12 8:37am
-

Last edited:
01/19/12
8:41amGabriel Reguly says: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,
GabrielPrevious versions of this answer: 01/19/12 at 8:41am
This question has expired.
Gabriel Reguly, Charles Klycinski, Francisco Javier Carazo Gil, robster voted on this question.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
