首页 > 解决方案 > 用 ACF 替换网站徽标

问题描述

我在 WordPress 上使用 Astra 主题,并尝试用 ACF 字段条目替换站点徽标。使用 Astra 的 astra_replace_header_logo 过滤器在设置徽标时遇到了很大的困难。尝试从小步骤开始并用硬编码的 url 或附件 ID 替换徽标,但似乎都不起作用。

function custom_astra_replace_header_logo($image) { 
$custom_logo_nav_id = get_field('logo_nav', 'options', false); 
if($custom_logo_nav_id) { 

$image[0] = '/wp-content/uploads/2021/05/new-logo.png'; 
return $image; 
} 
} 

add_filter( 'astra_replace_header_logo', 'custom_astra_replace_header_logo' );

这似乎没有任何效果。我还尝试将附件 ID 与大小一起使用。Astra 的函数建议返回 url 应该没问题(https://github.com/brainstormforce/astra/blob/master/inc/extras.php#L260)。

有趣的是,如果我将 $image 设置为奇怪的东西,例如附件 ID,$image = 5412 ;我会在前端得到一个损坏的图像src(unknown)

真的不明白为什么这不起作用。有人对此有经验吗?非常感谢任何帮助,谢谢。

标签: phpwordpressfunctionwordpress-themingadvanced-custom-fields

解决方案


弄清楚出了什么问题。我使用名为 WP Offload Media 的插件自动将媒体文件上传到 AWS,出于某种原因重写了徽标的 URL,并且它始终使用原始的 Astra 站点徽标。即使该功能正在运行。当我禁用卸载媒体时,该功能按预期工作。现在只需要弄清楚为什么它正在做它正在做的事情:)这是我最后使用的函数:

function custom_astra_replace_header_logo( $image ) { 
$custom_logo_nav_id = get_field('logo_nav', 'options'); 
    if($custom_logo_nav_id == true){ 
        
        $image[0] = $custom_logo_nav_id; 
        return $image; 
    } 

    return $image; 
} 

add_filter( 'astra_replace_header_logo', 'custom_astra_replace_header_logo' );


推荐阅读