put default image if there is no image using ACF

三世轮回 提交于 2020-01-07 02:21:30

问题


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


来源:https://stackoverflow.com/questions/37895633/put-default-image-if-there-is-no-image-using-acf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!