【问题标题】:How to change get_the_post_thumbnail_url output with filter or raw php如何使用过滤器或原始 php 更改 get_the_post_thumbnail_url 输出
【发布时间】:2020-12-21 10:51:58
【问题描述】:

这是我的问题:

我想改变这个函数的输出:

$thumb = get_the_post_thumbnail_url();

URL 将返回如下内容:

http://mywebsite.local/wp-content/uploads/myimage.png

我的目标只是将 URL 的第一部分更改为:

http://myurl.local/wp-content/uploads/myimage.png

我总是可以使用 str_replace:

$otherthumb = str_replace('http://mywebsite.local', 'http://myurl.local', $thumb);

但我希望能找到一些已经出炉的东西。

有什么建议吗?

提前谢谢你!

【问题讨论】:

    标签: php wordpress filter action


    【解决方案1】:

    您可以使用 wordpress wp_get_attachment_image_src 过滤钩子。

    function change_image_url( $image, $attachment_id, $size, $icon ){
        $otherthumb = str_replace('http://mywebsite.local', 'http://myurl.local', $image[0]);
        return $otherthumb;
    }
    add_filter( 'wp_get_attachment_image_src', 'change_image_url', 10, 4 );
    

    【讨论】:

    • 基本上,我会将链接包装在一个函数中,然后我将添加过滤器?
    • 是的,但是这个过滤器在你激活的主题functions.php中并改变你的URL。
    猜你喜欢
    • 2012-06-29
    • 2012-09-21
    • 2013-11-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多