HTML生成者the_post_thumbnail()
&;get_the_post_thumbnail()
可以使用post_thumbnail_html
滤器即使没有后期缩略图,也可以这样做。示例:
/**
* Filters the post thumbnail HTML.
*
* @since 2.9.0
*
* @param string $html The post thumbnail HTML.
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default \'post-thumbnail\'.
* @param string $attr Query string of attributes.
*/
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Optionally add any logic here for determining what markup to output.
// $html will be an empty string if there is no post thumbnail.
$new_html = \'<img src="https://placekitten.com/g/600/600" alt="kitten">\';
return $new_html;
}
add_action( \'post_thumbnail_html\', \'wpse_post_thumbnail_html\', 10, 5 );
上述解决方案要求主题调用
the_post_thumbnail()
, 但是,当帖子没有与之关联的缩略图时,它将起作用。
请注意,如果帖子没有缩略图,则上述解决方案在下面的示例中不起作用。这里,条件语句阻止has_post_thumbnail()
从被调用:
// the_post_thumbnail() will only be called if the post has a thumbnail.
if ( has_post_thumbnail() ) {
the_post_thumbnail( \'large\' );
}
替代技术(改变现有特征图像的src值)
wp_get_attachment_image_src
过滤器允许
src
要修改的附件的。
此功能强大的过滤器会影响所有附件映像src值,因此有必要添加/删除附加到的回调函数wp_get_attachment_image_src
基于所需条件。
这个begin_fetch_post_thumbnail_html
和end_fetch_post_thumbnail_html
为此,行动是完美的。
下面是一些基于帖子id更改特征图像的示例代码。您可以修改此代码以使用任何必要的逻辑来确定wpse_wp_get_attachment_image_src
应添加和删除回调。
/**
* Fires before fetching the post thumbnail HTML.
*
* Provides "just in time" filtering of all filters in wp_get_attachment_image().
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width
* and height values (in that order). Default \'post-thumbnail\'.
*/
function wpse_begin_fetch_post_thumbnail_html( $post_id, $post_thumbnail_id, $size ) {
// For example, if the post id is 1479, we will add our callback to modify the image.
// You could use whatever logic you need to determine if the filter should be added.
if ( 1479 === $post_id ) {
add_filter( \'wp_get_attachment_image_src\', \'wpse_wp_get_attachment_image_src\', 10, 4 );
}
}
add_action( \'begin_fetch_post_thumbnail_html\', \'wpse_begin_fetch_post_thumbnail_html\', 10, 3 );
/**
* Fires after fetching the post thumbnail HTML.
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width
* and height values (in that order). Default \'post-thumbnail\'.
*/
function wpse_end_fetch_post_thumbnail_html( $post_id, $post_thumbnail_id, $size ) {
// Now we remove the callback so that it only affects the desired post.
if ( 1479 === $post_id ) {
remove_filter( \'wp_get_attachment_image_src\', \'wpse_wp_get_attachment_image_src\', 10, 4 );
}
}
add_action( \'end_fetch_post_thumbnail_html\', \'wpse_end_fetch_post_thumbnail_html\', 10, 3 );
/**
* Filters the image src result.
*
*
* @param array|false $image Either array with src, width & height, icon src, or false.
* @param int $attachment_id Image attachment ID.
* @param string|array $size Size of image. Image size or array of width and height values
* (in that order). Default \'thumbnail\'.
* @param bool $icon Whether the image should be treated as an icon. Default false.
*/
function wpse_wp_get_attachment_image_src( $image, $attachment_id, $size, $icon ) {
$image[0] = \'https://placekitten.com/g/600/600\';
return $image;
}