WordPress Fallback缩略图图像的大小(WordPress Fallback Thumbnail image by size)

编程入门 行业动态 更新时间:2024-10-27 14:21:24
WordPress Fallback缩略图图像的大小(WordPress Fallback Thumbnail image by size)

我有多个自定义缩略图大小。

例如:

<?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

本文发布于:2023-07-07 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩略图   图像   大小   Fallback   WordPress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!