我在WC上为我的产品创建了5个自定义元值,但并非每个产品都具有所有自定义字段。
此刻,我正在显示元数据,如下所示:
<div class="meta">
<?php if($product->get_meta('metabox_animal') != '') echo '<div class="type"><p>Row One</p></div>' . $product->get_meta('metabox_animal'); ?>
<?php if($product->get_meta('metabox_medical') != '') echo '<div class="type"><p>Row Two</p></div>' . $product->get_meta('metabox_medical'); ?>
<?php if($product->get_meta('metabox_general') != '') echo '<div class="type"><p>Row Three</p></div>' . $product->get_meta('metabox_general'); ?>
<?php if($product->get_meta('metabox_capacity') != '') echo '<div class="type"><p>Row Four</p></div>' . $product->get_meta('metabox_capacity'); ?>
<?php if($product->get_meta('metabox_pet') != '') echo '<div class="type"><p>Row Five</p></div>' . $product->get_meta('metabox_pet'); ?>
</div>
有没有一种方法可以创建一个循环遍历所有元值的循环,如果它们存在,则显示它们,但如果它们为空/空/不使用,则显示一个容器“不适用”?
任何帮助将不胜感激!
答案 0 :(得分:0)
这未经测试,只有一种方法可以完成:
<?php
$html = '';
// collection of meta keys and their display label
$meta_keys_with_labels = [
['animal', 'Row One'],
['medical', 'Row Two'],
['general', 'Row Three'],
['capacity', 'Row Four'],
['pet', 'Row Five']
];
$html .= '<div class="meta">';
// loop to check if meta key/value exists and append to html string
foreach ($meta_keys_with_labels as $meta) {
// unpack the key and display label
[$key, $label] = $meta;
// grab meta value
$meta_value = $product->get_meta("metabox_$key");
// append meta value to html string
if (!empty($meta_value)) {
$html .= "<div class=\"type\"><p>$label</p></div>$meta_value";
}
}
$html .= '</div>';
echo $html;
答案 1 :(得分:0)
当然!
您可以创建一个数组,其中包含您需要检索的所有元术语。
在您的代码之后,这是您需要的代码。当然,您可以根据需要添加/删除。
<?php
$meta_terms = [
'metabox_animal',
'metabox_medical',
'metabox_general',
'metabox_capacity',
'metabox_pet'
];
foreach ($meta_terms as $meta_term) {
$meta = $product->get_meta($meta_term);
if (!empty($meta)) {
echo '<div class="type"><p>' . $meta . '</p></div>';
}
}
那么...我们在这里干了什么?
首先,我们创建了一个需要检索的所有元数据的数组。
创建完该数组后,我们需要开始迭代以获取,检查和显示每个元数据。
为此,我们使用foreach(PHP foreach)。
好
在foreach块中,使用包含需要检索的元字符串的变量$ meta_term,现在使用它来获取产品元数据。
现在,我们使用!empty()函数检查元数据是否为空,如果不为空,则我们知道可以输出值,因此,使用echo我们现在可以构建适当的html。
希望此帮助= D
答案 2 :(得分:0)
您可以在数组中设置相关数据,然后按如下所示使用foreach循环,如果自定义字段值为空或不存在,则将显示“ NOT APPLICABLE”(不适用)(代码带有注释) :
<?php
// Data array with Meta key / Label pairs
$data = array(
'metabox_animal' => __("Row One"),
'metabox_medical' => __("Row Two"),
'metabox_general' => __("Row Three"),
'metabox_capacity' => __("Row Four"),
'metabox_pet' => __("Row Five"),
);
echo '<div class="meta">';
// Loop through the data array of Meta key / Label pairs
foreach ( $data as $meta_key => $label ) {
$meta_value = $product->get_meta($meta_key); // Get the meta value
$meta_value = empty($meta_value) ? __("NOT APPLICABLE") : $meta_value; // If empty show "NOT APPLICABLE"
echo '<div class="type"><strong>' . $label . '</strong>: ' . $meta_value . '</div>';
}
echo '</div>';
?>
我更改了您的html。如果这不方便并且您想保留自己的,请更换:
echo '<div class="type"><strong>' . $label . '</strong>: ' . $meta_value . '</div>';
通过此行:
echo '<div class="type"><p>' . $label . '</p></div>' . $meta_value;