简体   繁体   中英

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. 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.

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.

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. Or if I can access the key value "id => 653" without switching the output.

Thanks.

The output-string is probably serialized (easier for Wordpress to store data more efficient).

Try:

<?php maybe_unserialize( $original ) ?>

If you want to know more about this look at: http://codex.wordpress.org/Function_Reference/maybe_unserialize

$ var = json_decode($ testing4);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM