Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

php - put default image if there is no image using ACF

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 } ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$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 } ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...