2013-05-21 85 views
3

我想設置一個小部件,按類別過濾我的帖子。 比方說,我確實有兩個不同的類別,分別是「國家」和「停留時間」。這裏是什麼,我有一個例子:按多個複選框類別過濾帖子 - wordpress

enter image description here

我想要什麼,是由多個分類篩選職位。因此,如果用戶正在查看「老撾」國家以及「2-4天」的停留時間,我想只檢索已添加類別「老撾」「2-4天」類別的帖子。

我試圖用Query Multiple Taxonomies pluging。但是,這個插件是 檢索所有職位與「老撾」類別和所有職位的停留時間「2-4天」。

我知道,我可以過濾後用此查詢,但是,我需要一些幫助來創建這個小部件與提交按鈕。此外,我想定製它以刪除父類別並將它們顯示爲標題(移除「國家」和「停留時間」複選框,並向它們添加特定類)?

工作查詢:

<?php // cat 42=Laos  cat 57=2-4Days 
    <?php $my_query_1 = new WP_query(array('category__and' => array(42,57))); ?> 
    <?php while ($my_query_1->have_posts()) : $my_query_1->the_post(); ?> 
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
    <?php the_title(); ?>"><?php the_title(); ?><?php the_excerpt(); ?></a> 
    <?php endwhile; ?> 

感謝您的幫助

我做了什麼:

  1. 我創建了一個新的側邊欄在那裏我要添加新的形式
  2. 我創建了這個新邊欄的模板「sidebar-filters.php」。這裏是我在裏面的代碼。

    <div id="secondary" class="widget-area" role="complementary"> 
    I'm the sidebar filters 
    <?php 
    
        $current_countries = get_query_var('countries'); 
        $current_stays = get_query_var('stays'); 
    
        $countries = get_categories('child_of=62'); 
        $stays = get_categories('child_of=63'); 
    ?> 
    
    <form action="<?php the_permalink(); ?>" id="filterForm" method="post"> 
         <ul> 
           <li> 
            <label><b>Countries</b></label> 
           </li><br/> 
           <?php 
           foreach ($countries as $country) { 
    //     var_dump($country->cat_ID); 
    //     var_dump($country->name); 
            echo sprintf('<li><input type="checkbox" name="countries[]" id="checkbox_%s" value="%s" %s',$country->name , $country->cat_ID, set_checked($current_countries, $country)); 
            echo sprintf('/><label for="checkbox_%s">%s</label></li>',$country->name,$country->name); 
           } 
           ?>   
         </ul><br/><br/> 
         <ul> 
           <li> 
             <label><b>Length of stay</b></label> 
           </li><br/> 
           <?php 
           foreach ($stays as $stay) { 
    //     var_dump($stay->cat_ID); 
    //     var_dump($stay->slug); 
            echo sprintf('<li><input type="checkbox" name="stays[]" id="checkbox_%s" value="%s" %s',$stay->slug , $stay->cat_ID, set_checked($current_stays, $stay)); 
            echo sprintf('/><label for="checkbox_%s">%s</label></li>',$stay->slug,$stay->name); 
           } 
           ?>        
         </ul><br/><br/>  
         <ul> 
           <li> 
             <button type="submit">Send email</button> 
           </li> 
         </ul> 
         <input type="hidden" name="submitted" id="submitted" value="true" /> 
    </form> 
    

  3. 這裏是我在function.php有

功能my_query_vars($瓦爾) { array_push(代碼$瓦爾, '國家', '停留'); return $ vars; } add_filter('query_vars','my_query_vars');

function set_checked($ arr,$ value) { $ checked =''; if(!空($ arr)& & in_array($ value,$ arr))$ checked ='checked'; return $ checked; }

function my_query($query) 
{ 
    // You can use is_archive() or whatever you need here 
    if ($query->is_main_query() && is_search()) { 

    $cat_and = array(); 

    foreach (array('countries', 'stays') as $variable) { 
     $categories = get_query_var($variable); 

     if (!empty($categories)) { 
     $cat_and = array_merge($cat_and, $categories); 
     } 
    } 

    if (!empty($cat_and)) $query->set('category__and', $cat_and); 
    } 
} 
add_action('pre_get_posts', 'my_query'); 

But, I'm retrieving nothing for the moment, what do I am doing wrong? 

回答

1
<?php 
    $checkboxArray = $_POST['checkbox']; //checkbox[] is name of all your checkboxes 
    $catIds = implode(',',$checkboxArray); 
    $my_query = new WP_Query('showposts=10&cat='.$catIds); ?> 

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 

    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> 

    <?php the_title(); ?></a> 

    <?php the_content(); ?> //optional, can also be the_excerpt 

    <?php endwhile; ?> 

You can change the value of showposts as you required. 
+0

我發佈的查詢正在工作,但我想知道如何根據選定的複選框構建和更新此查詢? – Romain

+0

查看更新回答 –

+0

感謝您的回答,但我想知道的是如何使用提交按鈕創建此小部件? 我該如何定製我的小部件? – Romain

2

這裏的基本思想與形式要做到這一點:

首先,你必須註冊新的查詢乏,所以你可以使用GET和具有在URL中的過濾器(即你可以稍後重寫,如果需要的話):

add_filter('query_vars', 'my_query_vars'); 
function my_query_vars($vars) 
{ 
    array_push($vars, 'countries', 'stays'); 
    return $vars; 
} 

然後創建一個窗體並使用數組名稱爲每組複選框打印類別。您需要確保恢復提交表單後檢查的複選框,您可以在同一頁面上執行此操作。

function set_checked($arr, $value) 
{ 
    $checked = ''; 
    if (!empty($arr) && in_array($value, $arr)) $checked = 'checked'; 
    return $checked; 
} 

$current_countries = get_query_var('countries'); 
$current_stays = get_query_var('stays'); 

$countries = get_categories('child_of=id_for_countries'); 
$stays = get_categories('child_of=id_for_length-of-stay'); 

// Begin form 

foreach ($countries as $country) { 
    echo sprintf('<input type="checkbox" name="countries[]" value="%s" %s', $country->cat_ID, set_checked($current_countries, $country)); 
} 

foreach ($stays as $stay) { 
    echo sprintf('<input type="checkbox" name="stays[]" value="%s" %s/>', $stay->cat_ID, set_checked($current_stays, $stay)); 
} 

// End form 

最後,您需要修改查詢根據這個變量來過濾:

add_action('pre_get_posts', 'my_query'); 
function my_query($query) 
{ 
    // You can use is_archive() or whatever you need here 
    if ($query->is_main_query() && is_search()) { 

    $cat_and = array(); 

    foreach (array('countries', 'stays') as $variable) { 
     $categories = get_query_var($variable); 

     if (!empty($categories)) { 
     $cat_and = array_merge($cat_and, $categories); 
     } 
    } 

    if (!empty($cat_and)) $query->set('category__and', $cat_and); 
    } 
} 

我沒有測試它,但這應該工作,希望這有助於。

+0

我正在努力 – Romain

+0

我更新了我的問題,如果你可以看一下,這將非常好,謝謝 – Romain

+1

我想你可能不得不使用父ID來代替slug來選擇子類別。像'get_categories('child_of = 123')'這裏檢查文檔http://codex.wordpress.org/Function_Reference/get_categories – elclanrs

相關問題