You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中echo语句无法正确输出$logo_url变量问题求助

解决WordPress中图片遮罩URL无法正确赋值的问题

问题出在ACF函数的使用逻辑上:the_field('site_logo_review_section')是直接输出字段内容,而非返回值。你把它赋值给$logo_url时,变量实际是空的,导致图片URL被提前输出到页面,而-webkit-mask-image的url()里没有内容。

修正方法很简单,把the_field()替换为get_field()——这个函数会返回字段值,不会直接输出:

function wpc_elementor_shortcode( $atts ) {
    // 用get_field获取字段值,而非直接输出
    $logo_url = get_field('site_logo_review_section');
    // 增加判断,避免字段为空时输出无效样式
    if ( $logo_url ) {
        echo "<style>#logo-mask{
        min-height: 350px;
        -webkit-mask-image: url(" . esc_url($logo_url) . ");
        mask-image: url(" . esc_url($logo_url) . ");
        -webkit-mask-size: contain;
        mask-size: contain;
        -webkit-mask-position: center center;
        mask-position: center center;
        -webkit-mask-repeat: no-repeat;
        mask-repeat: no-repeat;}</style>";
    }
}
add_shortcode( 'logo_mask', 'wpc_elementor_shortcode');

额外优化说明:

  • 用esc_url()对URL进行转义,避免注入风险,提升代码安全性
  • 补充标准的mask-*属性,兼容更多现代浏览器
  • 增加字段非空判断,避免字段为空时输出无用的style标签

修改后,$logo_url就能正确获取到图片地址并填充到遮罩属性中,不会再出现URL提前输出的问题。

内容的提问来源于stack exchange,提问作者samir elwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 06:15:12