在Woocommerce中添加特定的属性和值以输入和标记html标签

时间:2018-08-14 08:19:45

标签: php html wordpress woocommerce attributes

我正在尝试添加样式,具体取决于是否单击了该复选框。该代码缺少输入和标签的“ id”和“ for”(第11行)。逻辑决定增加数字的产生。如何正确执行?

foreach ($choices as $choice) {
    $attr = '';
    $key_val = explode("|", $choice);
    /* It has to be two items ( Value => Label ), otherwise don't proceed */
    if (count($key_val) == 2) {
        if (in_array(trim($key_val[0]), $defaults)) {
            $attr = 'checked';
        }
        /* For admin field, we don't need <li></li> wrapper */
        $html .= (($_ptype != "wccaf") ? '<li>' : '') . '<input type="checkbox" data-has_field_rules="'.$has_field_rules.'" data-is_pricing_rules="'.$_is_pricing_rules.'" class="' . $_ptype . '-field ' . $_class . '" name="' . esc_attr($_meta["name"] . $_index) . '[]" value="' . esc_attr(trim($key_val[0])) . '" ' . $attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory" ' . $_ptype . '-mandatory="' . $_meta["required"] . '" ' . $_readonly . ' /><label class="wcff-option-wrapper-label">' . esc_attr(trim($key_val[1])) . '</label>' . (($_ptype != "wccaf") ? '</li>' : '');
    }
}

1 个答案:

答案 0 :(得分:1)

已更新

尝试以下操作(未经测试),这将为for添加一个<label>属性+值,并为id添加一个<input>属性+值:

foreach ($choices as $choice) {
    $attr = '';
    $key_val = explode("|", $choice);
    /* It has to be two items ( Value => Label ), otherwise don't proceed */
    if (count($key_val) == 2) {
        if (in_array(trim($key_val[0]), $defaults)) {
            $attr = 'checked';
        }
        $sprintf = sprintf( '<input type="checkbox" %s %s %s %s %s %s %s /><label %s class="wcff-option-wrapper-label">%s</label>',
            'id="' . esc_attr($_meta["name"] . $_index) . '"',
            'data-has_field_rules="'.$has_field_rules.'"',
            'data-is_pricing_rules="'.$_is_pricing_rules.'"',
            'class="' . $_ptype . '-field ' . $_class . '"',
            'name="' . esc_attr($_meta["name"] . $_index) . '[]"',
            'value="' . esc_attr(trim($key_val[0])) . '"',
            $attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory' . $_meta["required"] . '" ' . $_readonly,
            'for="' . esc_attr($_meta["name"] . $_index) . '"',
        esc_attr(trim($key_val[1]) ) );

        $html .= $_ptype != "wccaf" ? '<li>'.$sprintf.'</li>' : $sprintf;
    }
}

我将代码嵌入到sprintf()函数中,以使其更具可读性,功能性和易于调整。