Link to home
Start Free TrialLog in
Avatar of Member_2_7966984
Member_2_7966984

asked on

How to write if else statement that loads image from custom field in WordPress else loads a place holder image if no custom image is chosen

So I tried something like this but it did not work...


			<?php 
			foreach( $myposts as $post ) :	setup_postdata($post); ?>

			<? if ( get_post_meta($post->ID, "wpcf-header-image", true) ) { 
				$store_imgs = get_post_meta($post->ID, "wpcf-header-image", true); 
			}  else {
				$store_imgs = "/wp-content/uploads/2018/12/Products_Banner_BG800px-tall-2.jpg"; 
			}
			?>	
											
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?= $store_imgs; ?>'); background-position:center center; background-size:cover;">
</div>

Open in new window

Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Use the following and see what it prints out:

<?php
                  foreach( $myposts as $post ) :      setup_postdata($post); ?>

                  <? if ( get_post_meta( get_the_ID(), 'wpcf-header-image', true ) ) {
                        $store_imgs = get_post_meta($post->ID, "wpcf-header-image", true);
                                var_dump($store_imgs);
                  }  else {
                        $store_imgs = "/wp-content/uploads/2018/12/Products_Banner_BG800px-tall-2.jpg";
                  }
                  ?>      
                                                                  
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?php echo $store_imgs; ?>'); bac
Avatar of Member_2_7966984
Member_2_7966984

ASKER

So I tried the code provided I ended up getting a 502 error 

User generated image

User generated image
Try your code again but with the Var_Dump, like this and let me know what is printed:

<?php
                  foreach( $myposts as $post ) :      setup_postdata($post); ?>

                  <? if ( get_post_meta($post->ID, "wpcf-header-image", true) ) {
                        $store_imgs = get_post_meta($post->ID, "wpcf-header-image", true);
echo '<pre>';
var_dump($store_imgs);
                  }  else {
                        $store_imgs = "/wp-content/uploads/2018/12/Products_Banner_BG800px-tall-2.jpg";
                  }
                  ?>      
                                                                  
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?= $store_imgs; ?>'); background-position:center center; background-size:cover;">
</div>
I think there is something wrong with this code - It breaks the site - So First I need the code not to break the site before I can tell you what is outputted - I get the the same 502 error as before using the code you had in the above suggestion 
SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial