【问题标题】:Advice on Error: Array to string conversion. Wordpress关于错误的建议:数组到字符串的转换。 WordPress
【发布时间】:2015-04-19 15:49:42
【问题描述】:

我一直在寻找解决此错误的方法。我只是想要一些建议,因为我认为这是我缺少的一些小东西。

我正在尝试使用灵活的内容从使用 ACF pro 设置的选项页面获取 url 字段和图像字段。我正在获取数据,但不是作为字符串变量。

这是错误的屏幕截图。 新链接http://redrocketwebsitedesign.co.uk/Array_error2.png

以及生成它的代码。

    <ul class="list-inline"><li>
    <p>Follow us on</p>
  </li>
  <?php //Options: Social icons

// check if the flexible content field has rows of data
if( have_rows('social_icon', 'option') ):

     // loop through the rows of data
    while ( have_rows('social_icon', 'option') ) : the_row();

        if( get_row_layout() == 'social_icon' ):


$image = get_sub_field_object('social_image', 'option');

if( $image ){
?>


<li>
<a href="<?php echo the_sub_field('social_link', 'option'); ?>" target="_blank">
<img class="img img-responsive" src="<?php echo $image[7]; ?>" alt="<?php echo $image[alt]; ?>"/>
</a> 
<?php var_dump('Vardump <br />') ?>
<?php var_dump($url) ?>
<?php var_dump('<br /> Image array <br />') ?>
<?php var_dump($image) ?>
<?php var_dump('<br /> Image link <br />') ?>
<?php var_dump($image['7']) ?>
</li>

<?php 
}

endif;

    endwhile;
    else :

    // no layouts found

endif;

  ?>


</ul>

任何帮助将不胜感激:-]

$social_image 是 $field_name,存储为图像数组。当我 print_r $image 时,这就是结果。我想我已经接近能够从数组中选择正确的字符串了。

    Image array

`" array(22) { ["ID"]=> int(138) ["key"]=> string(19) "field_54e47a9b7f735" ["label"]=> string(12) "Social Image" ["name"]=> string(12) "social_image" ["prefix"]=> string(0) "" ["type"]=> string(5) "image" ["value"]=> array(18) { ["ID"]=> int(105) ["id"]=> int(105) ["title"]=> string(11) "Tripadvisor" ["filename"]=> string(24) "footertripadvisorNEW.png" ["url"]=> string(83) "http-link" ["alt"]=> string(11) "Tripadvisor" ["author"]=> string(1) "1" ["description"]=> string(0) "" ["caption"]=> string(0) "" ["name"]=> string(20) "footertripadvisornew" ["date"]=> string(19) "2015-02-10 16:13:27" ["modified"]=> string(19) "2015-02-18 11:52:15" ["mime_type"]=> string(9) "image/png" ["type"]=> string(5) "image" ["icon"]=> string(68) "http://localhost:8888/wordpress/wp-includes/images/media/default.png" ["width"]=> int(50) ["height"]=> int(50) ["sizes"]=>`

..等等。

【问题讨论】:

  • 能否提供api-template.php文件的第895行?
  • 我已经修改了这个问题。在 api-template 的第 900 行下面开始 /* get_sub_field_object()。我想我应该使用这个函数而不是 get_sub_field。

标签: arrays wordpress while-loop options advanced-custom-fields


【解决方案1】:

get_sub_field() 接受string 类型的单个参数。你目前传递的太多了。您应该使用:

$value = get_sub_field( $field_name );

另外,如上所述,您需要确保 $field_name 实际上是一个字符串,而不是一个数组。

【讨论】:

  • 嗨,Mevius,我已经更新了上面的代码和错误图像。 $field_name 是存储为数组的图像。我认为 $get_sub_field_object 可能会起作用。因为我可以从数组中提取我需要的字符串,但它目前还没有工作。如果您对我有任何建议,那就太好了:-]
  • 嗨梅维斯,谢谢。我的错误是使用 $get_sub_field_object 而不是 $get_sub_field。
【解决方案2】:

您的问题是您在线上使用的 2 个变量之一

$value = get_sub_field( $field_name, $format_value );

当函数需要一个字符串时是一个数组。

你能不能在前面的那一行添加这段代码并复制粘贴输出...

print_r( $field_name ); echo '<br>'; print_r( $format_value );

这样我们就可以知道这些变量中存储了什么。

【讨论】:

  • 嗨 dahbahrami,我已将 $print_r 信息复制到问题的底部。我也更新了代码。我现在只有 $image 的问题。我已经修复了 $social_href。任何建议都会很棒。
猜你喜欢
  • 2017-12-02
  • 1970-01-01
  • 2015-02-26
  • 2012-07-15
  • 2019-05-01
  • 2016-09-07
  • 2022-01-25
  • 2012-12-02
  • 1970-01-01
相关资源
最近更新 更多