模板中的Meta Box多个复选框

时间:2015-07-02 作者:rwzdoorn

我用过this check meta box 在我的功能中。php,但我不明白如何获取元数据。我正在尝试输出产品是否仍然可用。

Metabox设置:

/* Array of the meta box options. */
$meta_boxes = array(
    \'affiliate-url\' => array( \'name\' => \'Affiliate_url\', \'title\' => __(\'Affiliate URL (generated)\', \'hybrid\'), \'type\' => \'text\' ),  
    \'original-url\' => array( \'name\' => \'Original_url\', \'title\' => __(\'Original URL\', \'hybrid\'), \'type\' => \'text\' ),         
    \'product-id\' => array( \'name\' => \'Product_ID\', \'title\' => __(\'Product ID\', \'hybrid\'), \'type\' => \'text\' ),           
    \'normal-price\' => array( \'name\' => \'Normal_price\', \'title\' => __(\'Normal Price\', \'hybrid\'), \'type\' => \'text\' ),     
    \'discount-price\' => array( \'name\' => \'Discount_price\', \'title\' => __(\'Discount Price\', \'hybrid\'), \'type\' => \'text\' ),
    \'sizes-available\' => array(
        \'name\' => \'Sizes_available\',
        \'title\' => __(\'Currently available:\', \'hybrid\'),
        \'type\' => \'check\',
        \'options\' => array(
            \'XS\' => \'Extra Small\',
            \'S\' => \'Small\',
            \'M\' => \'Medium\',
            \'L\' => \'Large\',
            \'XL\' => \'Extra Large\',
            \'XXL\' => \'Extra Extra Large\',                                               
),
模板设置:

                            <div class="col-md-6 col-xs-6 product-info">
                                <h5>AVAILABLE IN</h5>
                                <ul>
                                        <li>Small</li>
                                        <li><strike>Medium</strike> <small>(sold out)</small></li>
                                        <li>Large</li>
                                        <li>Xtra Large</li>
                                        <li>Xtra Xtra large</li>    
                                </ul>                                                                                                                   
                            </div>
我需要输出列表并放置<strike>$size</strike> <small> (sold out)</small>.

我通常使用<?php if ( get_post_meta($post->ID, \'Sizes_available\', true) ) : ?> 检查元框中是否填写了某些内容,但这不适用于“选中”输出。

有人能帮忙吗?

1 个回复
最合适的回答,由SO网友:marcovega 整理而成

这将在模板的侧面进行。首先将所有大小放入一个数组:

$allSizes = array(

    \'XS\' => \'Extra Small\',
    \'S\' => \'Small\',
    \'M\' => \'Medium\',
    \'L\' => \'Large\',
    \'XL\' => \'Extra Large\',
    \'XXL\' => \'Extra Extra Large\',

);

echo \'<ul>\';
$outStock = get_post_meta($post->ID, \'Sizes_available\', true);
然后,让我们比较一下其中一种尺寸是否缺货,以及相应的产量:

foreach($allSizes as $key => $size){
    if(in_array($key, $outStock)){
        echo "<li><strike>$size</strike> <small>(sold out)</small></li>";
    }
    else{
        echo "<li>$size</li>";
    }
}
echo "</ul>";

结束

相关推荐

保存Metabox自定义字段值

我使用此代码显示帖子类型下拉列表,以选择可用的帖子类型,但当我提交帖子时,它会将值还原回帖子,如何确保它会保存它?<select name=\'my_meta_box_post_type\' id=\'my_meta_box_post_type\'> <?php $post_types=get_post_types(\'\', \'objects\'); foreach ($post_types as $post_type): ?>