我有多个自定义缩略图大小。
例如:
<?php add_image_size( 'sizeOne', 300, 100 ); add_image_size( 'sizeTwo', 600, 200 ); ?>我用功能显示缩略图:
<?php the_post_thumbnail( 'sizeOne' ); ?>并且,如果图像不存在(如果我没有在管理区域中设置自定义缩略图),则后备图像将为default-thumbnail.png 。 此过程由functions.php的此代码生成:
<?php function filter_post_thumbnail_html( $html ) { // If there is no post thumbnail, // Return a default image if ( '' == $html ) { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png" width="300" height="100" />'; } // Else, return the post thumbnail return $html; } add_filter( 'post_thumbnail_html', 'filter_post_thumbnail_html' ); ?>问题是所有缩略图大小的后备图像都是相同的。 因为它是300x100,它对于sizeOne是完美的,但是对于sizeTwo而言太小了。
如何“检查”缩略图是否为sizeOne或sizeTwo ,然后返回正确的后备图像?
-
Ps:我知道另一种方法, 但这不是我想要的 。
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'sizeOne' ); } else { ?> <img src="<?php bloginfo('template_directory'); ?>/images/default-thumbnail.jpg" width="300" height="100" /> <?php } ?>I have more than one custom thumbnail size.
For example:
<?php add_image_size( 'sizeOne', 300, 100 ); add_image_size( 'sizeTwo', 600, 200 ); ?>I show the thumbnail with the function:
<?php the_post_thumbnail( 'sizeOne' ); ?>And, if the image does not exist (if I haven't set the custom thumbnail in Admin area), the fallback image will be default-thumbnail.png. This process is made by this code in functions.php:
<?php function filter_post_thumbnail_html( $html ) { // If there is no post thumbnail, // Return a default image if ( '' == $html ) { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png" width="300" height="100" />'; } // Else, return the post thumbnail return $html; } add_filter( 'post_thumbnail_html', 'filter_post_thumbnail_html' ); ?>The problem is that the fallback image is the same for all thumbnail sizes. As it's 300x100, it is perfect to sizeOne, but is too small for sizeTwo.
How can I "check" if the thumbnail asked is sizeOne or sizeTwo and then return the correct fallback image?
–
Ps: I know the other way to do this, but that's not what I want.
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'sizeOne' ); } else { ?> <img src="<?php bloginfo('template_directory'); ?>/images/default-thumbnail.jpg" width="300" height="100" /> <?php } ?>最满意答案
您可以使用过滤器的其他参数。
<?php function filter_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size ) { // If there is no post thumbnail, // Return a default image if ( '' == $html ) { if ( 'sizeTwo' == $size ) { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail2.png" width="600" height="200" />'; } else { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png" width="300" height="100" />'; } } // Else, return the post thumbnail return $html; } add_filter( 'post_thumbnail_html', 'filter_post_thumbnail_html', 10, 4 );You can use the additional parameters of the filter.
<?php function filter_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size ) { // If there is no post thumbnail, // Return a default image if ( '' == $html ) { if ( 'sizeTwo' == $size ) { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail2.png" width="600" height="200" />'; } else { return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png" width="300" height="100" />'; } } // Else, return the post thumbnail return $html; } add_filter( 'post_thumbnail_html', 'filter_post_thumbnail_html', 10, 4 );更多推荐
php,thumbnail,image,电脑培训,计算机培训,IT培训"/> <meta name="descripti
发布评论