php - Wordpress 格式 meta_value 列以仅获取选项值
问题描述
在 wordpress 中,我为自定义字段创建了一个选择值,如下所示:
我正在尝试获取 magento 中的选择值
field_57bdd83367bb1 是我的字段键
public function getPostType2()
{
try{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('new_db');
$query = 'SELECT meta_value FROM ' . $resource->getTableName('wp_postmeta'). ' WHERE meta_key = "field_57bdd83367bb1"';
$results = $readConnection->fetchAll($query);
return $results[0]['meta_value'];
}catch (Exception $e) {
return true;
}
}
在 phtml 中:
$recentpost = Mage::helper('wordpress')->getblogmainbannerpost($bannercheckedblogpost['post_id']);
$posttype= Mage::helper('wordpress')->getPostType2();
$posttype = explode(',',$posttype);
echo 'Post type:';print_r($posttype);
输出:
数组 ( [0] => a:12:{s:3:"key";s:19:"field_57bdd83367bb1";s:5:"label";s:4:"Type";s:4:"name ";s:4:"type";s:4:"type";s:6:"select";s:12:"instructions";s:0:"";s:8:"required";s :1:"0";s:7:"选择";a:7:{s:7:"文章";s:7:"文章";s:9:"博客文章";s:9:" blog_post";s:16:"Artists & Makers";s:16:"artistsandmakers";s:6:"视频";s:6:"视频";s:12:"在新闻界";s:12 :"in_the_press";s:14:"你知道吗?";s:12:"did_you_know";s:14:"词汇表 A - Z";s:8:"词汇表";}s:13:"default_value ";s:39:"blog_post in_the_press did_you_know ";s:10:"allow_null";s:1:"0";s:8:“多个”;s:1:“1”;s:17:“条件逻辑”;a:3:{s:6:“状态”;s:1:“0”;s:5:“规则” ;a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}} s:8:"allorany";s:3:"all";}s:8:"order_no";i:0;})
如何正确格式化上述输出,以便在我的下拉列表中仅获取文章、博客文章、艺术家和制造商等
<div class="category-list">
<?php // echo $this->__('All Types') ?>
<select id="blogcat">
<option><?php echo $this->__('Article Type') ?></option>
<option value=""><?php echo $this->__('All Types..') ?></option>
<option value="<?php echo $posttype;?>"> </option>
</select>
</div>
解决方案
我相信您看到的数据格式是PHP的serialize() unserialize()函数的结果。所以你会改变return $results[0]['meta_value'];
为return (unserialize($results[0]['meta_value']))->choices;
推荐阅读
- python - 无法从课堂上刮取文字(BeautifulSoup)
- algorithm - 回溯算法解决数独问题的时间复杂度
- c# - 调用 C++ DLL 时 C# IL 如何通过 Ref 取回参数
- vue.js - 我在 Vuejs 中有太多观察者,我的代码不可扩展
- unity3d - C# Unity:无法将方法组“GetComponent”转换为非委托类型“Rigidbody2D”
- c - 将 char* 传递给 pthread 崩溃
- mysql - MySQL - GROUP_CONCAT 在 WHERE | 小于或大于值
- python - pybind11 加速函数调用
- mysql - 如何从 MySql 5.5 中删除重复记录。派生表,按日期聚合数据
- c# - 注册为windows服务时无法访问netcore服务