我有如下这样的自定义选项存储在数据库中:
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"}
我正在尝试在前端显示一些此类信息,
$shop_options = get_option('shop_details');
echo $shop_options['shop_description'];
但是我收到此错误:
Warning: Illegal string offset 'shop_description' in D:\www\my-shop\wp-content\themes\shop-city\taxonomy-shop.php on line 32 {
我在做什么错了?
答案 0 :(得分:0)
您在$shop_options
中获得的值是 JSON 格式,因此必须先对其进行解码,然后才能获取数组。使用:
$shop_options = get_option('shop_details');
$shop_options = json_decode($shop_option);
如果调用json_decode($somestring)
,将得到一个对象,您需要像$object->key
那样进行访问;但是,如果您调用json_decode($somestring, true)
,则将得到一个字典,并且可以像{{1} }