缩略图大小基于中定义的尺寸WordPress Dashboard > Settings > Media, 除非你的主题覆盖了它。
如何使我的主题覆盖它?
要更改默认的后期缩略图大小,您需要使用set_post_thumbnail_size
. 将此添加到函数中。php文件:
if ( function_exists( \'add_theme_support\' ) ) {
add_theme_support( \'post-thumbnails\' );
set_post_thumbnail_size( W, H, true ); //change to false to disable hard cropping
}
更改
W
和
H
要为缩略图设置的尺寸(以像素为单位)。例如
set_post_thumbnail_size( 150, 150 );
将缩略图大小设置为150 x 150像素。
NOTE: 检查主题是否已经add_theme_support( \'post-thumbnails\' );
— 如果有,只需添加set_post_thumbnail_size( W, H );
在它下面。
因为我需要的图库缩略图大小与帖子缩略图不同,所以没有专门针对图库的其他功能。
选项1:(所有代码都进入functions.php)
首先,让我们为库缩略图注册新的图像大小:
if ( function_exists( \'add_image_size\' ) ) {
// set gallery thumbnail image\'s size to 250 x 250 pixels
add_image_size( \'ilyad-gallery-thumb\', 250, 250, true ); //change to false to disable hard cropping
}
现在,让我们用自己的代码修改默认的WordPress图库代码(以便我们可以为图库中的图像设置自己的缩略图大小)-下面的所有代码都是默认的图库短代码,取自
wp-includes/media.php
. 我改变了
\'size\' => \'thumbnail\'
到
\'size\' => \'gallery-thumb\'
要让画廊使用我们新注册的画廊图像大小。
// Modified Gallery Shortcode
add_filter("post_gallery", "wpse56909_post_gallery",10,2);
function wpse56909_post_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
// We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
if ( isset( $attr[\'orderby\'] ) ) {
$attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
if ( !$attr[\'orderby\'] )
unset( $attr[\'orderby\'] );
}
extract(shortcode_atts(array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\',
\'id\' => $post->ID,
\'itemtag\' => \'dl\',
\'icontag\' => \'dt\',
\'captiontag\' => \'dd\',
\'columns\' => 3,
\'size\' => \'gallery-thumb\',
\'include\' => \'\',
\'exclude\' => \'\'
), $attr));
$id = intval($id);
if ( \'RAND\' == $order )
$orderby = \'none\';
if ( !empty($include) ) {
$include = preg_replace( \'/[^0-9,]+/\', \'\', $include );
$_attachments = get_posts( array(\'include\' => $include, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( \'/[^0-9,]+/\', \'\', $exclude );
$attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
} else {
$attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
}
if ( empty($attachments) )
return \'\';
if ( is_feed() ) {
$output = "\\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? \'right\' : \'left\';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = \'\';
if ( apply_filters( \'use_default_gallery_style\', true ) )
$gallery_style = "
<style type=\'text/css\'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
$output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class=\'gallery-item\'>";
$output .= "
<{$icontag} class=\'gallery-icon\'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class=\'wp-caption-text gallery-caption\'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= \'<br style="clear: both" />\';
}
$output .= "
<br style=\'clear: both;\' />
</div>\\n";
return $output;
}
这些更改不会影响您的旧帖子。如果你认为应该,你需要重新生成所有帖子的缩略图,这是最受欢迎的插件-
AJAX Thumbnail Rebuild.
[Source]
选项2:(所有代码都进入functions.php)
假设您希望库中缩略图的大小为250 x 250像素。转到
WordPress Dashboard > Settings > Media 并设置
Thumbnail size 宽度和高度分别为250和250。完成一半!
现在,我们需要修改代表您帖子的缩略图的大小。允许首先注册新的缩略图大小:
if ( function_exists( \'add_image_size\' ) ) {
add_image_size( \'ilyad-post-thumb\', 150, 150, true ); //change to false to disable hard cropping
}
现在,应该修改主题文件中帖子缩略图的所有代码实例,以使用
ilyad-post-thumb
大小这里有两个例子。
- CASE - 1: 如果您的主题使用以下内容:
<?php
if ( \'\' != get_the_post_thumbnail() ) :
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), thumbnail );
?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'reddle\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark">
<img class="featured-image" src="<?php echo $thumbnail[0]; ?>" alt="">
</a>
</div>
应修改为:<?php
if ( \'\' != get_the_post_thumbnail() ) :
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ilyad-post-thumb );
?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'reddle\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark">
<img class="featured-image" src="<?php echo $thumbnail[0]; ?>" alt="">
</a>
</div>
在上面,我们只是更改thumbnail
(图像大小)至ilyad-post-thumb
. - CASE - 2: 如果您的主题使用以下内容:
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
将其更改为:<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(\'ilyad-post-thumb\');
}
?>
这两个例子应该给你一个想法,告诉你应该如何去做PS: 这个答案是在我的知识范围内写的