2017-08-02 69 views
0

我正在尋找一種更加智能的方式(除了使用if-statement)來獲取wordpress部件中複選框的總數。count selected selected複選框php wp部件

我的代碼如下所示(簡化):

public function widget($args, $instance) { 

    $no = count($_GET['widget-columns']) ; 

    echo '<div class="uk-child-width-1-'.esc_attr($no).'">'; 
    echo '...'; 
    echo '</div>'; 


} 

而且這裏的複選框

public function form($instance) { ?> 

    <p> 
     <input id="..." name="column-value-1 widget-columns[]" type="checkbox" value="1" <?php checked('1', $variable_column_one); ?>/> 
     <label for="...">Show Column 1</label> 
    </p> 
    <p> 
     <input id="..." name="column-value-2 widget-columns[]" type="checkbox" value="1" <?php checked('1', $variable_column_two); ?>/> 
     <label for="...">Show Column 2</label> 
    </p> 
    <p> 
     <input id="..." name="column-value-3 widget-columns[]" type="checkbox" value="1" <?php checked('1', $variable_column_three); ?>/> 
     <label for="...">Show Column 3</label> 
    </p> 

<?php } 

這將導致undefined index widget-columns(原因很明顯)。

任何幫助,讓我在正確的軌道上獲得檢查複選框的計數,非常感謝。

回答

0

name屬性中不能有兩個值。將您的<input name="column-value-2 widget-columns[]">更改爲只需<input name="widget-columns[]">

+0

做完之前,但它仍然無法正常工作。現在我只需使用'$ no ='0'; if($ variable_column_one === 1){$ no + = 1;無論如何,謝謝。 :) – kiarashi