what is your code to show the content of the custom fields?
something like this should work:
<?php
if( get_post_meta($post->ID, 'video', true) ) {
echo get_post_meta($post->ID, 'video', true);
} else {
echo get_post_meta($post->ID, 'image', true);
}
?>
http://codex.wordpress.org/Function_Reference/get_post_meta
<?php if ( get_post_meta($post->ID, 'Video', true) ) { ?>
<?php echo get_post_meta($post->ID, "Video", true); ?>
<?php } else { ?><?php } ?>
<?php if ( get_post_meta($post->ID, 'Image', true) ) { ?>
That’s what I have. The thing is on single.php I only one to show the video if any. If not then show the picture.
your code should actually nearly do that.
<?php
if( get_post_meta($post->ID, 'Video', true) ) {
echo get_post_meta($post->ID, 'Video', true);
} else {
if( get_post_meta($post->ID, 'Image', true) ) {
echo get_post_meta($post->ID, 'Image', true);
}
}
?>
Yeah but that’s still not what I’m looking for. Let’s say that If I’ve added both Video and Image, on single.php I only want to see Video and not both.
If Video is missing then it should go to Image.
Video + Image = Video
what is it doing at the moment?
showing both?
or showing nothing?
do you have a link where this can be seen?
(from the logic, the above code should work)
however, here is a variation of the above:
<?php
if( get_post_meta($post->ID, 'Video', true) != '' ) {
echo get_post_meta($post->ID, 'Image', true);
}
echo get_post_meta($post->ID, 'Video', true);
?>
Here is a link to the page:
http://www.mym2webdesign.com/futbolalrojovivo/alemania/prueba/
Currently it shows both the video and the image.
EDIT: I’m currently testing out your latest code.
Here is what I have:
<?php
if( get_post_meta($post->ID, 'Video', true) != '' ) {
echo get_post_meta($post->ID, 'Image', true);
}
echo get_post_meta($post->ID, 'Video', true);
?>
<img align="top" alt="<?php the_title(); ?> <?php _e('thumbnail'); ?>" src="<?php echo get_post_meta($post->ID, "Image", true); ?>" style="width: 590px; height: 315px;" title="<?php the_title(); ?>" />
<?php } else { ?><?php } ?>
You just need an else. I do something similar on one of my sites. This is slightly different but you can see what I am doing:
<?php if($src_featured_video1 !== '') { ?> <!-- If there is a video display the video --!>
<a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
<script type="text/javascript" src="<?php echo(get_bloginfo('template_directory')); ?>/js/swfobject.js"></script>
<script type="text/javascript">
var s1 = new SWFObject("<?php echo(get_bloginfo('template_directory')); ?>/js/player.swf","play","640","480","9","#FFFFFF");
s1.addParam("allowfullscreen","true");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=<?php bloginfo('url'); ?><?php echo $src_featured_video1; ?>&image=<?php bloginfo('url'); ?><?php echo $src_featured_image1; ?>_lg.jpg&backcolor=FFFFFF&autostart=true&&screencolor=FFFFFF&border=true");
s1.write("container");
</script>
<?php } else { ?> <!-- If there isn't video, display image 1 --!>
<?php if($src_featured_image1 !== '') { ?>
<img src="<?php bloginfo('url'); ?><?php echo $src_featured_image1; ?>_lg.jpg" width="600px" />
<?php } ?><br />
<?php if($src_featured_caption1 !== '') { ?>
<?php echo $src_featured_caption1; ?><br /><br />
<?php } ?>
<?php } ?>
It first checks if there is a video URL in a custom field, if there isn’t it loads the image. Then it checks for info in the other various fields which are echoed if they have content.
Basically what I do is paste the whole <object></object> into the custom field. So I don’t need to write everything every time I write a post. Just paste the embed.
@futbolalrojovivo
thanks for posting your full code – it shows that the output of your image customfield is outside the if statement;
this should correct it (use it instead of the code you posted):
<?php
if( get_post_meta($post->ID, 'Video', true) ) {
echo get_post_meta($post->ID, 'Video', true);
} elseif( get_post_meta($post->ID, 'Image', true) ) { ?>
<img align="top" alt="<?php the_title(); ?> <?php _e('thumbnail'); ?>" src="<?php echo get_post_meta($post->ID, "Image", true); ?>" style="width: 590px; height: 315px;" title="<?php the_title(); ?>" />
<?php } ?>
first it checks if a video is in the respective custom field, and if yes, shows the vides;
if not, it checks if a image is in the respective custom field, and if yes, it shows the image;
otherwise nothing is shown.
hope this works, good luck 😉
It works great! Thank you for all your help! Now you should go and take a break, watching the world cup!