如何在WordPress短代码中传递数组?及数组转字符串报错解决
解决WordPress短代码返回数组的报错问题
首先得说清楚你遇到的报错根源:WordPress短代码的回调函数强制要求返回字符串类型的值,而你的ff_register_shortcode()最后直接返回了$danger数组,WordPress尝试把数组转换成字符串输出时,就触发了Array to string conversion这个通知。
接下来针对不同的需求场景,给你两种可行的解决方案:
场景1:需要把数组数据渲染成HTML输出
如果你的目标是把箱子数据展示成前端可见的内容(比如列表、表格),那就在短代码函数内部把数组遍历转换成HTML字符串,再返回这个字符串就行。
修改后的完整代码:
function ff_register_shortcode(){ $fedex_feeding= array(); $BoxLength=esc_attr( get_option('ff_list_boxes') ); // 循环获取所有箱子数据 for($f=0 ; $f<$BoxLength;$f++){ array_push($fedex_feeding, array( 'name' => get_option('ff_name_'.$f), 'id' => get_option('ff_id_'.$f), 'max_weight' => get_option('ff_max_weight_'.$f), 'box_weight' => get_option('ff_box_weight_'.$f), 'length' => get_option('ff_length_'.$f), 'width' => get_option('ff_width_'.$f), 'height' => get_option('ff_height_'.$f), 'inner_length' => get_option('ff_inner_length_'.$f), 'inner_width' => get_option('ff_inner_width_'.$f), 'inner_height' => get_option('ff_inner_height_'.$f), 'type' => get_option('ff_type_'.$f), ) ); } // 将数组转换成可输出的HTML字符串 $output = '<div class="fedex-box-container">'; $output .= '<h3>Fedex 可用箱子列表</h3>'; $output .= '<ul class="box-list">'; foreach($fedex_feeding as $box){ $output .= '<li class="box-item">'; $output .= '<strong>箱子名称:</strong>' . esc_html($box['name']) . '<br>'; $output .= '<strong>箱子ID:</strong>' . esc_html($box['id']) . '<br>'; $output .= '<strong>最大承重:</strong>' . esc_html($box['max_weight']) . '<br>'; // 可以根据需求继续添加其他字段的展示 $output .= '</li>'; } $output .= '</ul></div>'; return $output; // 返回字符串而非数组 } add_shortcode('ff_arrays' , 'ff_register_shortcode');
场景2:需要把数组传递给其他函数/模板使用
如果你的需求不是直接输出,而是要把这个数组交给其他业务逻辑处理(比如主题模板、自定义函数),可以用以下两种方法:
方法1:使用全局变量传递
在短代码函数里把数组赋值给全局变量,之后在需要的地方调用这个全局变量即可:
function ff_register_shortcode(){ global $fedex_boxes_global; // 声明全局变量 $fedex_feeding= array(); $BoxLength=esc_attr( get_option('ff_list_boxes') ); for($f=0 ; $f<$BoxLength;$f++){ array_push($fedex_feeding, array( 'name' => get_option('ff_name_'.$f), 'id' => get_option('ff_id_'.$f), 'max_weight' => get_option('ff_max_weight_'.$f), 'box_weight' => get_option('ff_box_weight_'.$f), 'length' => get_option('ff_length_'.$f), 'width' => get_option('ff_width_'.$f), 'height' => get_option('ff_height_'.$f), 'inner_length' => get_option('ff_inner_length_'.$f), 'inner_width' => get_option('ff_inner_width_'.$f), 'inner_height' => get_option('ff_inner_height_'.$f), 'type' => get_option('ff_type_'.$f), ) ); } $fedex_boxes_global = $fedex_feeding; // 把数组赋值给全局变量 return ''; // 返回空字符串避免报错 } add_shortcode('ff_arrays' , 'ff_register_shortcode');
之后在需要使用数组的地方(比如主题模板文件):
global $fedex_boxes_global; if(!empty($fedex_boxes_global) && is_array($fedex_boxes_global)){ // 这里写你的数组处理逻辑 foreach($fedex_boxes_global as $box){ // 执行操作 } }
方法2:序列化数组后传递
把数组序列化成字符串返回,使用时再反序列化还原成数组:
function ff_register_shortcode(){ $fedex_feeding= array(); $BoxLength=esc_attr( get_option('ff_list_boxes') ); for($f=0 ; $f<$BoxLength;$f++){ array_push($fedex_feeding, array( 'name' => get_option('ff_name_'.$f), 'id' => get_option('ff_id_'.$f), 'max_weight' => get_option('ff_max_weight_'.$f), 'box_weight' => get_option('ff_box_weight_'.$f), 'length' => get_option('ff_length_'.$f), 'width' => get_option('ff_width_'.$f), 'height' => get_option('ff_height_'.$f), 'inner_length' => get_option('ff_inner_length_'.$f), 'inner_width' => get_option('ff_inner_width_'.$f), 'inner_height' => get_option('ff_inner_height_'.$f), 'type' => get_option('ff_type_'.$f), ) ); } return serialize($fedex_feeding); // 返回序列化后的字符串 } add_shortcode('ff_arrays' , 'ff_register_shortcode');
使用时还原数组:
$serialized_boxes = do_shortcode('[ff_arrays]'); $fedex_boxes = unserialize($serialized_boxes); if(is_array($fedex_boxes)){ // 处理数组逻辑 }
另外提个小优化:你原代码里的$danger数组完全是冗余的,直接$danger = $fedex_feeding;就能拿到相同的结果,没必要再循环一遍赋值,以后写代码可以注意减少这种无意义的重复操作。
内容的提问来源于stack exchange,提问作者Zaheer Abbas
相关产品推荐
相关产品推荐

