WordPress get_post_meta返回一个字符串,如何访问单个键值


WordPress get_post_meta returns a string how do I access the single key value

我正在尝试修复一些代码并显示使用自定义元框设置的图像。我在wp_postmeta中找到了保存的数据,看起来数据被保存为字符串,但我可以看到一个明显的键值对。

当我使用以下代码时...

 $imgVar = get_post_meta($post->ID, 'attachments', true);
 $testing4 = $imgVar;
 var_dump($testing4);

。我得到以下输出...

string(101) "{"my_item":[{"id":"653","fields":{"title":"mytitle","caption":"test this out"}}]}"

。这看起来像是在告诉我输出是一个包含 101 个字符的字符串,但我看到了键值和一个数组。

我想要输出的是,或者看起来应该是什么......

array[0](
  "my_item" => array(
         "id" => "653",
         "fields" => array(
                "title" =>"mytitle",
                "caption" => "test this out"
            ),
    )
),

有人可以解释一下这个新手:)输出的内容,以及是否可以将输出的内容转换为常规数组。或者,如果我可以在不切换输出的情况下访问键值"id => 653"。

谢谢。

输出字符串可能是序列化的(Wordpress更容易更有效地存储数据)。

尝试:

<?php maybe_unserialize( $original ) ?>

如果您想了解更多有关此内容的信息,请查看:http://codex.wordpress.org/Function_Reference/maybe_unserialize

$var = json_decode($testing 4);

使用 <pre> 标签格式化输出

echo '<pre>' . var_dump($testing4) . '</pre>';