如果在PHP中没有选择任何内容,如何设置默认图像?

时间:2014-08-10 12:26:31

标签: php html wordpress

我正在尝试制作一个物业租赁网站,如果没有上传属性照片,我希望它设置默认图像。就像现在一样,如果没有上传图像就会出错,我需要更改它,所以它改为使用默认图像。

以下是特色图片的php脚本的代码片段:

        /* Upload Images */
        if($_FILES){
            foreach( $_FILES as $submitted_file => $file_array ){
                if( is_valid_image( $_FILES[$submitted_file]['name'] ) ){
                    $size = intval( $_FILES[$submitted_file]['size'] );
                    if( $size > 0 ){
                        if( $submitted_file == 'featured_image' ){
                            /* Featured Image */
                            $uploaded_file_id = insert_attachment( $submitted_file, $property_id, true );

                            /* Virtual Tour Image */
                            if(empty($tour_video_image) && !empty($_POST['video-url'])){
                                update_post_meta($property_id, 'REAL_HOMES_tour_video_image', $uploaded_file_id);
                            }

                        }else{
                            /* Gallery Images */
                            $uploaded_file_id = insert_attachment( $submitted_file, $property_id );
                            add_post_meta($property_id, 'REAL_HOMES_property_images', $uploaded_file_id);
                        }
                    }
                }else{
                    /* Skip the image upload if image do not has a valid file extension */
                }

关于精选图片的另一个片段:

<div class="form-option">
    <label for="featured-image"><?php _e('Property Featured Image','framework'); ?></label>
    <div id="featured-thumb-container" class="clearfix">
    <?php
        if( has_post_thumbnail( $target_property->ID ) ){
            echo '<div class="gallery-thumb">';
            echo get_the_post_thumbnail( $target_property->ID, 'thumbnail' );
            echo '<a class="remove-featured-image" data-property-id="'.$target_property->ID.'" href="'. site_url("/wp-admin/admin-ajax.php") .'" ><i class="fa fa-trash-o"></i></a>';
            echo '<span class="loader"><i class="fa fa-spinner fa-spin"></i></span>';
            echo '</div>';
            }
            ?>
             </div>
                <div id="featured-file-container" class="<?php if(has_post_thumbnail( $target_property->ID )){ echo "hidden"; }?>" >
                <input id="featured-image" name="featured_image" type="file" title="<?php _e( '* Please provide image with proper extension! Only .jpg .gif and .png are allowed.!', 'framework'); ?>" class="image required" required/>
                 <div class="field-description">
                    <?php _e('Image should have minimum width of 770px and minimum height of 386px. ( Bigger image will be cropped automatically )','framework'); ?>
                 </div>
                </div>
            </div>

我希望有一个简单的解决方案,以便脚本不需要上传特色图像,而是在没有上传的情况下设置默认图像。

谢谢。

更新: 大家好,感谢您的评论。我应该提到这是一个wordpress主题,所以它不是由我编写的,而且我对PHP的经验很少。

无论如何,我认为缩略图在​​这里被调用:

<?php
if ( has_post_thumbnail() ){
    $image_id = get_post_thumbnail_id();
    $image_url = wp_get_attachment_url($image_id);
    ?>
    <figure>
        <span class="format-icon image"></span>
        <a href="<?php echo $image_url; ?>" class="<?php echo get_lightbox_plugin_class(); ?>" title="<?php the_title(); ?>">
            <?php
            if( is_page_template( 'template-home.php' )){
                the_post_thumbnail('gallery-two-column-image');
            }else{
                the_post_thumbnail('post-featured-image');
            }
            ?>
        </a>
    </figure>
    <?php
}
?>

我感谢任何帮助。谢谢。

更新2:更新了第一个被剪辑的代码

好的,所以我尝试使用空的else语句来设置默认图像。虽然我无法让它发挥作用。我也尝试添加这一行:if (empty($submitted_file)) $submitted_file = "path/default.jpg";,但仍然没有。关于如何使这项工作的任何建议?

/* Upload Images */
                if($_FILES){
                    foreach( $_FILES as $submitted_file => $file_array ){
                        if( is_valid_image( $_FILES[$submitted_file]['name'] ) ){
                            $size = intval( $_FILES[$submitted_file]['size'] );
                            if( $size > 0 ){
                                if( $submitted_file == 'featured_image' ){
                                    /* Featured Image */
                                    $uploaded_file_id = insert_attachment( $submitted_file, $property_id, true );
                if (empty($submitted_file)) $submitted_file = "path/default.jpg";

                                    /* Virtual Tour Image */
                                    if(empty($tour_video_image) && !empty($_POST['video-url'])){
                                        update_post_meta($property_id, 'REAL_HOMES_tour_video_image', $uploaded_file_id);
                                    }

                                }else{
                                    /* Gallery Images */
                                    $uploaded_file_id = insert_attachment( $submitted_file, $property_id );
                                    add_post_meta($property_id, 'REAL_HOMES_property_images', $uploaded_file_id);
                                }
                            }
                        }else{
                $submitted_file = 'path/default.jpg';
                            $uploaded_file_id = insert_attachment( $submitted_file, $property_id, true );
                        }
                    }
                }

0 个答案:

没有答案