我用过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) ) : ?>
检查元框中是否填写了某些内容,但这不适用于“选中”输出。
有人能帮忙吗?
最合适的回答,由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>";