我想设置一个小部件来过滤我的文章分类。比方说,我有两个不同的类别,一个是“国家”,另一个是“逗留时间”。下面是我所拥有的一个例子:

我想要的是,通过多个类别过滤帖子。因此,如果用户正在检查国家“老挝”和一段“2-4天”的逗留时间,我只想检索“老挝”和类别“2-4天”已经附加的帖子。
我试着使用查询多个分类插件。然而,这种插入正在检索所有“老挝”类别的帖子,以及所有停留时间为“2-4天”的帖子。
我知道,我可以使用这个查询过滤post,但是,我需要一些帮助来使用submit按钮创建这个小部件。另外,我想定制它以删除父类别,并将其显示为标题(删除"Countries“和”like of like“的复选框,并为其添加特定的类别)。
工作查询:
<?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; ?>谢谢你的帮助
我所做的:
函数my_query_vars($vars) { array_push($vars,'countries','stays');返回$vars;} add_filter('query_vars','my_query_vars');
函数set_checked($arr,$value) { $checked = '';if (!$checked ($arr)& in_array($value,$arr)) $checked=‘$value’;返回$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?发布于 2013-05-21 09:32:33
下面是使用表单进行此操作的基本思想:
首先,您必须注册新的查询vars,以便您可以使用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);
}
}我还没有测试它,但这应该是可行的,希望这会有所帮助。
发布于 2013-05-21 08:59:59
<?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.https://stackoverflow.com/questions/16665868
复制相似问题