Forums

[resolved] Multiple Custom Fields (12 posts)

  1. futbolalrojovivo
    Member
    Posted 1 year ago #

    I currently have two custom fields. One I use it for "Video" and the other for "Image". When I insert the url of an Image it shows it on the home.php and then in single.php. The problem is once I add something to the "Video" field and the "Image" field it shows both on the single.php.

    Basically what I need to know is if I can tell wordpress that when I insert info to both "Video" and "Image" it should cancel out "Image" on the single.php.

    Kind of like if "Video" then NOT "Image". Thanks for your time!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    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

  3. futbolalrojovivo
    Member
    Posted 1 year ago #

    <?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.

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    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);
      }
    }
    ?>
  5. futbolalrojovivo
    Member
    Posted 1 year ago #

    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

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    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);
    ?>
  7. futbolalrojovivo
    Member
    Posted 1 year ago #

    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.

  8. futbolalrojovivo
    Member
    Posted 1 year ago #

    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 } ?>
  9. misterpatrick67
    Member
    Posted 1 year ago #

    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.

  10. futbolalrojovivo
    Member
    Posted 1 year ago #

    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.

  11. alchymyth
    The Sweeper
    Posted 1 year ago #

    @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 ;-)

  12. futbolalrojovivo
    Member
    Posted 1 year ago #

    It works great! Thank you for all your help! Now you should go and take a break, watching the world cup!

Topic Closed

This topic has been closed to new replies.

About this Topic