[英]WordPress get_post_meta returns a string how do I access the single key value
I am trying to fix a bit of code and display an image that is set with a custom metabox. 我正在尝试修复一些代码,并显示使用自定义metabox设置的图像。 I have found the saved data in wp_postmeta and it looks like the data is saved as a string but I can see an obvious key value pair.
我在wp_postmeta中找到了保存的数据,看起来数据已保存为字符串,但是我可以看到一个明显的键值对。
When I use the following code... 当我使用以下代码时...
$imgVar = get_post_meta($post->ID, 'attachments', true);
$testing4 = $imgVar;
var_dump($testing4);
...I get the following output... ...我得到以下输出...
string(101) "{"my_item":[{"id":"653","fields":{"title":"mytitle","caption":"test this out"}}]}"
... this looks like it is telling me that the output is a string with 101 characters but I see key values and an array. ……这似乎告诉我输出是一个包含101个字符的字符串,但是我看到了键值和一个数组。
what I would like to have output is, or what it seems it should be... 我想要的输出是什么,或者看起来应该是什么...
array[0](
"my_item" => array(
"id" => "653",
"fields" => array(
"title" =>"mytitle",
"caption" => "test this out"
),
)
),
can someone explain what is being output for this newb :), and if it is possible to turn what is being output into a regular array. 有人可以解释一下此newb :)的输出内容,以及是否有可能将输出内容转换为常规数组。 Or if I can access the key value "id => 653" without switching the output.
或者,如果我可以访问键值“ id => 653”而无需切换输出。
Thanks. 谢谢。
The output-string is probably serialized (easier for Wordpress to store data more efficient). 输出字符串可能已序列化(Wordpress可以更轻松地存储数据)。
Try: 尝试:
<?php maybe_unserialize( $original ) ?>
If you want to know more about this look at: http://codex.wordpress.org/Function_Reference/maybe_unserialize 如果您想了解更多有关此信息,请访问: http : //codex.wordpress.org/Function_Reference/maybe_unserialize
$ var = json_decode($ testing4);
使用<pre>
标签格式化输出
echo '<pre>' . var_dump($testing4) . '</pre>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.