我想設置一個小部件,按類別過濾我的帖子。 比方說,我確實有兩個不同的類別,分別是「國家」和「停留時間」。這裏是什麼,我有一個例子:按多個複選框類別過濾帖子 - wordpress
我想要什麼,是由多個分類篩選職位。因此,如果用戶正在查看「老撾」國家以及「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; ?>
感謝您的幫助
我做了什麼:
- 我創建了一個新的側邊欄在那裏我要添加新的形式
我創建了這個新邊欄的模板「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>
這裏是我在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?
我發佈的查詢正在工作,但我想知道如何根據選定的複選框構建和更新此查詢? – Romain
查看更新回答 –
感謝您的回答,但我想知道的是如何使用提交按鈕創建此小部件? 我該如何定製我的小部件? – Romain