SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Post meta showing a blank image.

    Hi,

    I am using the following to code for two custom fields. When an image is inserted, there is no box underneath. If a video is inserted, a blank image/block shows up beneath it. There should be nothing. Im only inserting one or the other. I am pretty sure I am missing the echo part for the image if it is true, but I have tried several things with no luck. Any help is appreciated. I have included a screen shot as well..

    Code:
    <?php  if((get_post_meta($post->ID, "video", true))) { ?>
    				<?php echo get_post_meta($post->ID, "video", true); ?>
    				<?php } ?>	
    			
    				
    				<?php  if((get_post_meta($post->ID, "image", true))) { ?>
    			<?php
    				$images = array(
    				'example.jpg',
    				'example.jpg',
    				'example.jpg',
    				'example.jpg',
    				);
    				$image  = $images[array_rand($images)];
    				$output = "<img src=\"http://examplesite.com/images/random/" . $image . "\" />";
    				echo $output;
    			?>
    			<?php } ?>
    wtf.png

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If both are showing up, then clearly the culprit is

    if((get_post_meta($post->ID, "image", true)))

    returning true when it shouldn't.

    If both are supposed to be true, but you just don't want the image portion to run if a video is present, then you would change

    if((get_post_meta($post->ID, "image", true)))

    to

    else if((get_post_meta($post->ID, "image", true)))

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I figured the image is the problem, but I am not sure how to get it to display true only if it is true. If its not, it does nothing. The video is working properly.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I'm a little confused so bare with me here, from what i can gather you have two custom fields in which only one can be displayed at a time?

    If that's right then a question i have for you would be what would you like to happen if both fields have a value in it?

    I ask because the logic wonshikee used is doing exactly what you need it to do so why not just set a variable to say if "video" is present and "image" is present ignore "image".

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After rereading it, I have determined that you are correct and that I am an idiot. ... With that out the way, I am getting a unexpected T_ELSE when I change the "if"
    to "else if"

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    changed it up from the above recommendations, but still getting the blank image below the vid.

    Code:
    <?php  if((get_post_meta($post->ID, "video", true))) { ?>
    				<?php echo get_post_meta($post->ID, "video", true); ?>
    				
    				<?php } else if((get_post_meta($post->ID, "image", true))) { ?>
    			<?php
    				$images = array(
    				'example.jpg',
    				'example.jpg',
    				'example.jpg',
    				'example.jpg',
    				);
    				$image  = $images[array_rand($images)];
    				$output = "<img src=\"http://examplesite.com/images/random/" . $image . "\" />";
    				echo $output;
    			?>
    			<?php } ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The box was a style sheet issue. I guess it was just one of those days. Thanks to the both of you for the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •