2011-09-28 20 views
0

這可能是一個非常標準的事情。我無法爲我的生活做出如何去做。我查了各種其他編碼做類似的事情,但他們中的大多數似乎以不同於我的方式做事,我不太明白。Wordpress自定義小工具 - 保存從自定義帖子類型創建的複選框列表

基本上我正在創建一個簡單的自定義小部件。它從帖子類型中提取所有帖子,並將它們顯示爲複選框。我需要保存選中的帖子,然後將其作爲數組傳遞,以便我可以顯示選定的帖子。

在表單中我有顯示的複選框:

$postcount5 = 0; $featured_query5 = new WP_Query('showposts=5&post_type=adverts'); 
    while ($featured_query5->have_posts()) : $featured_query5->the_post(); 
    $do_not_duplicate[] = get_the_ID();$postcount5++; 
    $currentid5 = get_the_ID(); 
    echo '<p><label><input type="checkbox" name="adverts" value="'; 
    the_id(); 
    echo'" '; 
    if ($currentid5 == $adboxid) echo 'checked="yes"'; 
    echo '/> '; 
    the_title(); 
    echo' </label><br/></p>'; 

一次,我已經成功地挽救他們,我應該罰款。我只是不能解決如何保存動態創建的複選框列表。提前致謝。

回答

0

即使它不是動態的,它的代碼也不起作用。 你需要做的是重命名複選框的名稱,否則只能訪問最後一個值。例如。你可以做到這一點,像這樣:

$postcount5 = 0; 
$featured_query5 = new WP_Query('showposts=5&post_type=adverts'); 
while ($featured_query5->have_posts()) : $featured_query5->the_post(); 
    $do_not_duplicate[] = get_the_ID(); 
    $postcount5++; 
    $currentid5 = get_the_ID(); 

    echo '<p><label><input type="checkbox" name="adverts'.$postcount5.'" value="'.$the_id().'"; 
    if ($currentid5 == $adboxid) echo 'checked="yes"'; 
    echo '/> '; 
    the_title(); 
    echo' </label><br/></p>'; 

並最終獲得值,則需要一個提交按鈕,幷包裹了整個事情變成了一組「動作」元素,如:

<form name="postselector" action="whereever_you_want_the_user_to_go_next.php"> 
INSERT HERE ALL THE INPUT CHECKBOXES 
AND THE SUBMIT BUTTON 
</form> 

在whereever_you_want_the_user_to_go_next.php您終於可以通過以下方式閱讀所選項目:

if (isset($_POST['submit'])) { 
    $selectedposts = array(); 
    $i = 0; 
    foreach($_POST as $name => $value) { 
     if (preg_match('adverts',$name) { 
     $selectedposts[$i] = $value; 
     $i++; 
     } 
    } 
} 
相關問題