put default image if there is no image using ACF

2019-09-18 01:35发布

问题:

I am using Wordpress ACF plugin and I have custom products section and I put a custom field for featured image and it's working perfect.

Now I want if there is no featured image inserted then show a default image, I am using the following code but its not working.

<? php
$category_image = get_field('fimage');
if ($category_image) {
  $category_image_url = $category_image['sizes']['product-thumb'];
}
if ($category_image_url) { ?>
 <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/timthumb.php?src=<?php echo $category_image['url']; ?>&w=300&h=250&zc=0" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
} else($category_image_url) { ?>
  <img src="http://www.staticwhich.co.uk/static/images/products/no-image/no-image-available.png" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
<? php } ?>

回答1:

$category_image="";
$category_image_url="";
    $category_image = get_field('fimage');
    if($category_image){
        $category_image_url = $category_image['sizes']['product-thumb'];
    }
    if($category_image_url) { ?>
         <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/timthumb.php?src=<?php echo $category_image['url']; ?>&w=300&h=250&zc=0" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
    <?php
    } 
    else { ?>
       <img src="http://www.staticwhich.co.uk/static/images/products/no-image/no-image-available.png" alt="<?php echo $category_image['name']; ?>" title="<?php echo $category_image['name']; ?>">
                            <?php } ?>