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>
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/Product s_Banner_B G800px-tal l-2.jpg";
}
?>
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?= $store_imgs; ?>'); background-position:center center; background-size:cover;">
</div>
<?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/
}
?>
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?= $store_imgs; ?>'); background-position:center
</div>
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<?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/
}
?>
<div class="slide-img" style="width:100%; height: 800px; background-image: url('<?php echo $store_imgs; ?>'); bac