我不太确定我是否理解这个问题。您希望根据在中设置的字段显示不同的标记$atts
大堆如果是这种情况,那么应该这样做:
// Check so fields are set in the `$atts` array.
if (isset($atts[\'img\'], $atts[\'user\'], $atts[\'text\'])) {
return \'<div class="hire-equipment-item">
<div class="hire-equipment-item-img">
<img src="\' . esc_url($atts[\'img\']) . \'" height=200px" width="200px" alt="">
</div><br />
<div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>
<div class="hire-equipment-item-text">\' . sanitize_text_field($atts[\'text\']) . \'</div>
</div>\';
}
elseif (isset($atts[\'img\'], $atts[\'text\'], $atts[\'length\'], $atts[\'material\'], $atts[\'power\'])) {
// Second output goes here.
}
请注意
user
在第二种情况下不能设置,否则第一个条件将始终为真。
由于两种条件的标记非常相似,因此更好的方法可能是:
function hire_equipment_func($atts) {
// Store merged result.
$atts = shortcode_atts(array(
\'img\' => \'\',
\'user\' => \'\',
\'text\' => \'\',
\'length\' => \'\',
\'material\' => \'\',
\'power\' => \'\',
), array_change_key_case($atts, CASE_LOWER));
$output = \'<div class="hire-equipment-item>\';
// Check if image should be appended to output.
if (isset($atts[\'img\'])) {
$output .= \'<div class="hire-equipment-item-img">
<img src="\' . esc_url($attribs[\'img\']) . \'" height="200" width="200" alt="" />
</div>\';
}
// Check if user should be appended to output.
if (isset($atts[\'user\'])) {
$output .= \'<div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>\';
}
// Check if text should be appended to output.
if (isset($atts[\'text\'])) {
$output .= \'<div class="hire-equipment-item-text">\' . $atts[\'text\'] . \'</div>\';
}
// Check if we need to output more-information table.
if (isset($atts[\'length\']) || isset($atts[\'material\']) || isset($atts[\'power\'])) {
$output .= \'<div class="hire-equipment-more-information">
<table class="hire-equipment-more-information-table" cellpadding="15px">
<tr>
<th>Length:</th>
<th>Material:</th>
<th>Power:</th>
</tr>
<tr>
<td> \' . (isset($atts[\'length\']) ? $atts[\'length\'] : \'\') . \' </td>
<td> \' . (isset($atts[\'material\']) ? $atts[\'material\'] : \'\') . \' </td>
<td> \' . (isset($atts[\'power\']) ? $atts[\'power\'] : \'\') . \' </td>
</tr>
</table>
</div>\';
}
$output .= \'</div>\';
return $output;
}
您可能还需要对输出进行一些消毒。例如使用
esc_url()
对于URL和
sanitize_text_field()
用于字符串等。