简体   繁体   English

从 WordPress 选项表中获取自定义选项

[英]Get custom options from WordPress options table

I have custom options being stored in the database like this:我有自定义选项存储在数据库中,如下所示:

option_name: shop_details

option_value: {"shop_name":"My shop","shop_slug":"my-shop","shop_description":"This is my cool shop.","shop_start_date":"2019-10-03","shop_end_date":"2019-10-11","shop_owner":"1","taxonomy":"shop"}

I am trying to display some of this information on the front-end, like this:我正在尝试在前端显示一些此类信息,如下所示:

$shop_options = get_option('shop_details');
echo $shop_options['shop_description'];

But I am getting this error:但我收到此错误:

Warning: Illegal string offset 'shop_description' in D:\www\my-shop\wp-content\themes\shop-city\taxonomy-shop.php on line 32 {

What am I doing wrong?我究竟做错了什么?

The value you are getting in $shop_options is a JSON format, so you have to decode it first then you can get the array.您在$shop_options中获得的值是JSON格式,因此您必须先对其进行解码,然后才能获取数组。 Use:利用:

$shop_options = get_option('shop_details');
$shop_options = json_decode($shop_option);

If you call json_decode($somestring) you will get an object and you need to access like $object->key , but if you call json_decode($somestring, true) you will get a dictionary and can access like $array['key']如果你调用json_decode($somestring)你会得到一个 object 并且你需要像$object->key一样访问,但是如果你调用json_decode($somestring, true)你会得到一个字典并且可以像$array['key']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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