gpt4 book ai didi

php - Wordpress:如何 Hook get_the_post_thumbnail_url() 函数

转载 作者:行者123 更新时间:2023-12-01 22:05:15 28 4
gpt4 key购买 nike

我是 Wordpress 新手。我想要做的是 Hook get_the_post_thumbnail_url() 并返回略有不同的 URL。我知道我可以使用 WP add_action() 进行 Hook 。如果我编写我的函数并返回所需的字符串,我如何确保 get_the_post_thumbnail_url() 将返回我的自定义代码?

最佳答案

wp-includes/post-thumbnail-template.php中的get_the_post_thumbnail_url没有hooks和actions,定义如下:

function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
$post_thumbnail_id = get_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return false;
}
return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}

如果您遵循 wp_get_attachment_image_url 函数,您会发现它使用了 wp_get_attachment_image_src 函数,该函数会应用过滤器,因此您可以使用 wp_get_attachment_image_src< 为其创建过滤器

这将是您与该功能交互的唯一方式,尽管在功能树上稍靠上一点。

使用说明如下:

/**
* Filters the image src result.
*
* @since 4.3.0
*
* @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.
*/
apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );

关于php - Wordpress:如何 Hook get_the_post_thumbnail_url() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52332168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com