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

WordPress发票插件中路径与变量拼接失败问题求助

WordPress发票插件自定义模板加载图片问题解决

我在用一款支持自定义模板编辑的WordPress发票插件,想通过获取订单号变量拼接图片路径,在模板里加载对应图片,写了以下代码:

<?php 
    $order_id_image = $this->order_number();
    $order_id_image_num = strval($order_id_image);
    echo '<div>'.$order_id_image.'</div>';
    
    $file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/'.$order_id_image_num.".png";
    $base_image = file_get_contents($file_path_qr_image_base);
    echo '<div>'.$file_path_qr_image_base.'</div>';
    echo '<img src="data:image/gif;base64,' . $base_image . '" />';
    
    $order_id_image = $this->order_number();
    $file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/'.$this->order_number().".png";
?>

测试时图片加载异常,但使用静态路径(比如$file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/4213.png';)就能正常加载。

解决方案

排查后发现,当前场景中$this->order_number();返回值为null,相关错误日志可在/wp-content/debug.log文件中查看。最终用以下代码解决了问题:

$id = $order->get_id(); // 获取订单ID
$data = file_get_contents("/home/{your_site}/public_html/wp-content/uploads/folder/".$id.".png");
echo '<img src="data:image/png;base64,' . $data . '" style="height:250px">';

内容的提问来源于stack exchange,提问作者John Brown

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 12:02:25