2012-05-23 68 views
3

我的網站上的用戶上傳圖片比我的WordPress主題的基本縮略圖尺寸小。如何強制這些類型的圖像垂直或水平拉伸,因此沒有空白區域?如果上傳的圖片尺寸較小,如何強制使用縮略圖以適應上傳的圖片?

我的wp-admin>settings>media>thumbnail size設置爲124px x 156px。上傳的圖像是60px x 190px銷燬網站佈局。 www.sharehouse.co是測試網站。 下面的代碼是需要修改的。

<div class="post-left"> 
    <a href="<?php the_permalink(); ?>" <?php if ((get_option('cp_ad_image_preview') == 'yes') && (cp_get_image_url_raw($post->ID, 'large', 1) == true)) { ?>class="preview" rel="<?php echo cp_get_image_url_raw($post->ID, 'large', 1); ?>" <?php } ?>><?php if(get_post_meta($post->ID, 'images', true)) cp_single_image_legacy($post->ID, get_option('thumbnail_size_w'), get_option('thumbnail_size_h'), 'preview'); else cp_get_image_url_feat($post->ID, 'thumbnail', 'preview', 1); ?></a>     
</div> 

回答

3

所以,你需要做的第一件事就是剝去硬編碼的寬度和高度從img HTML是WordPress的創建屬性。它認爲你應該有這些尺寸並強制你使用它們。

來自https://wordpress.stackexchange.com/questions/5568/filter-to-remove-image-dimension-attributes

add_filter('post_thumbnail_html', 'remove_thumbnail_dimensions', 10); 
add_filter('image_send_to_editor', 'remove_thumbnail_dimensions', 10); 

function remove_thumbnail_dimensions($html) { 
    $html = preg_replace('/(width|height)=\"\d*\"\s/', "", $html); 
    return $html; 
} 

然後,它只是CSS

.attachment img { 
    width: 100%; /* scale to width of container, or whatever you want */ 
} 
+0

哇。反應快!那麼,我是否會將上面的代碼添加到我的問題中的代碼中?我在哪裏添加它?我發現代碼使用螢火蟲 - 我不是一個編碼器。 –

+0

你的'functions.php'文件中的第一位* *可能會出現在你的'functions.php'文件中,儘管它可能取決於你的網站的設置。它最有可能去那裏。 CSS會放在你的'style.css'文件中。雖然提供的CSS將使所有圖像適合它們的容器,但請確保將其更改爲您想要的方式。 – jeremyharris