首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按多个复选框类别过滤帖子- wordpress

按多个复选框类别过滤帖子- wordpress
EN

Stack Overflow用户
提问于 2013-05-21 08:55:21
回答 2查看 15.1K关注 0票数 3

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

我想要的是,通过多个类别过滤帖子。因此,如果用户正在检查国家“老挝”和一段“2-4天”的逗留时间,我只想检索“老挝”类别“2-4天”已经附加的帖子。

我试着使用查询多个分类插件。然而,这种插入正在检索所有“老挝”类别的帖子,以及所有停留时间为“2-4天”的帖子。

我知道,我可以使用这个查询过滤post,但是,我需要一些帮助来使用submit按钮创建这个小部件。另外,我想定制它以删除父类别,并将其显示为标题(删除"Countries“和”like of like“的复选框,并为其添加特定的类别)。

工作查询:

代码语言:javascript
复制
<?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. 我创建了这个新的边栏的模板“侧栏-filters.php”。这是我在里面的代码。
  3. 下面是我在我的function.php中的代码

函数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;}

代码语言:javascript
复制
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?
EN

回答 2

Stack Overflow用户

发布于 2013-05-21 09:32:33

下面是使用表单进行此操作的基本思想:

首先,您必须注册新的查询vars,以便您可以使用GET并在url中使用过滤器(如果需要,以后可以重写这些过滤器):

代码语言:javascript
复制
add_filter('query_vars', 'my_query_vars');
function my_query_vars($vars)
{
  array_push($vars, 'countries', 'stays');
  return $vars;
}

然后为每组复选框创建一个表单并使用数组名称打印类别。您需要确保还原在提交表单后选中的复选框,您可以在同一页上这样做。

代码语言:javascript
复制
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

最后,您需要根据以下变量修改查询以进行筛选:

代码语言:javascript
复制
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);
  }
}

我还没有测试它,但这应该是可行的,希望这会有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2013-05-21 08:59:59

代码语言:javascript
复制
   <?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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16665868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档