首页 > 解决方案 > 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>

标签: phpwordpressmagento

解决方案


我相信您看到的数据格式是PHP的serialize() unserialize()函数的结果。所以你会改变return $results[0]['meta_value'];return (unserialize($results[0]['meta_value']))->choices;


推荐阅读