【问题标题】:Notice: Array to string conversion in /var/www/html/mytheme/wp-includes/formatting.php on line 1025注意:第 1025 行 /var/www/html/mytheme/wp-includes/formatting.php 中的数组到字符串转换
【发布时间】:2017-08-21 23:57:27
【问题描述】:

我正在尝试使用 this script 在我的元框文件中添加变量而不是自定义字段 ID

我在 redux 框架中添加了一些选项,以提供更改自定义字段的可能性。

<?php
/*global from framework*/
global $redux;
/*custom fields options retrieved from redux framework*/
$custom_videourl = $redux['mytheme_videourl'];
$custom_duration = $redux['mytheme_duration'];
$custom_description = $redux['mytheme_desc'];
$fields = array(
    array(
     'label' => __( 'MP4/FLV & Youtube Url', 'framework' ),
     'desc' => __( 'Here you can add videos with mp4 format', 'framework' ),
     'id' => $custom_videourl,
     'type' => 'text'
    ),
    array(
     'label' => __( 'Video Duration', 'framework' ),
     'desc' => __( 'Example: 5:20', 'framework' ),
     'id' => $custom_duration,
     'type' => 'text'
    ),
    array(
     'label' => __( 'Video Description', 'framework' ),
     'id' => $custom_description,
     'desc' => __( 'Here you can write a description', 'framework' ),
     'type' => 'editor'
    )
);
$my_metaboxes = new custom_add_meta_box( 'mytheme_metaboxes', __( 'Video - Additional Information', 'framework' ), $fields, 'post', true );

但是通过上面的例子,我得到了 注意:数组到字符串的转换在 /var/www/html/mytheme/wp-includes/formatting.php 在第 1025 行

因此,如果我添加没有可变元框的自定义字段,则可以正常工作,如下所示:

$fields = array(
    array(
     'label' => __( 'MP4/FLV & Youtube Url', 'framework' ),
     'desc' => __( 'Here you can add videos with mp4 format', 'framework' ),
     'id' => 'mytheme_videourl',
     'type' => 'text'
    ),
    array(
     'label' => __( 'Video Duration', 'framework' ),
     'desc' => __( 'Example: 5:20', 'framework' ),
     'id' => 'mytheme_duration',
     'type' => 'text'
    ),
    array(
     'label' => __( 'Video Description', 'framework' ),
     'id' => 'mytheme_desc',
     'desc' => __( 'Here you can write a description', 'framework' ),
     'type' => 'editor'
    )
);
$my_metaboxes = new custom_add_meta_box( 'mytheme_metaboxes', __( 'Video - Additional Information', 'framework' ), $fields, 'post', true );

我尝试过使用 print_r 但元框不保存。有什么方法可以使第一个代码工作吗?使用变量而不是自定义字段 ID?

【问题讨论】:

  • 例如的内容是什么。 $redux['mytheme_videourl']; ?通知说它是一个数组,但你只想拥有一个特定的元素。
  • $redux['mytheme_videourl']; 是一个 redux 框架选项,它只是我的 redux / 主题设置面板中的一个字段。我想提供使自定义字段可变的可能性,就像你看到的那样,我从数据库中调用 global $redux 但是我只想在我的 metaboxes 文件中使用这些变量。Metaboxes 文件正在帖子编辑器下方创建自定义字段选项,通知也在帖子编辑器下方。
  • 是的,我知道,但$redux['mytheme_videourl']; 是一个数组,而不是一个字符串。你能在某个地方做一个print_r($redux['mytheme_videourl']),在那里你可以看到它的内容吗?或者print_r($redux['mytheme_videourl'], true)作为描述或类似的,这样你就可以看到它的内容?
  • 是的,我尝试过使用 print_r,但是当我这样做时,自定义字段值会消失。
  • 我要重新尝试

标签: php arrays wordpress meta-boxes


【解决方案1】:

您的某个 redux 变量似乎很可能包含一个数组而不是字符串。知道了这一点,您只需要弄清楚它是哪一个,并弄清楚您要查找的实际数据在哪里。

您可以进行调试的一种方法是将所有三个 redux 变量显式转换为字符串。 (例如'id' =&gt; implode("***", $custom_videourl))。然后,一旦您确定了哪个(或多个)是一个数组,您可能就会知道如何访问您真正想要的数据。

如果这不适合您,我建议将其添加到您的 wp-config.php 文件中:define( 'WP_DEBUG_LOG', true );

这将为您创建一个调试日志。然后您可以注销它(例如 error_log( print_r( $custom_videourl ) ); 我相信它通常将 debug.log 文件存储在 wp-content 文件夹中。

【讨论】:

  • 让我更好地解释一下,我的问题中的代码正在使用这个github.com/bainternet/My-Meta-Box 并且我在这个元框脚本中调用了 redux 变量,但 redux 变量只是 redux 框架中的文本字段
  • 当我从 Brainternet 脚本中删除(如在我的第二个代码中)并使用 get post meta 命令get_post_meta($post-&gt;ID, $custom_videourl, true); 调用它时,redux 变量工作正常,问题仅在元框字段中。我一直在调试,但是我会用内爆来看看你的解释
  • @Gazi 效果如何?我很想知道错误来自哪里。
  • 我用另一种方法解决了,因为它不能像在我的问题中那样使用我刚刚使用了我的第二个代码并创建了一个来自 redux 的自定义字段和一个来自 metabox 我不知道如何解释但它不可能回答我的问题,因为它不可能
  • 很抱歉,我这样做是为了防止其他人遇到类似问题。你是说你无法判断你的某个变量是否意外地有一个数组?
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 2021-12-28
  • 2023-03-06
  • 2015-03-26
  • 2022-01-05
  • 2013-08-14
  • 2013-11-03
相关资源
最近更新 更多