按多个复选框类别过滤帖子 - wordpress [英] Filter posts by multiple checkbox categories - wordpress

查看:25
本文介绍了按多个复选框类别过滤帖子 - wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个小部件来按类别过滤我的帖子.比方说,我确实有两个不同的类别,国家"和逗留时间"以及子类别.这是我所拥有的示例:

我想要的是按多个类别过滤帖子.所以,如果用户正在检查国家老挝"和2-4天"的逗留时间,我只想检索类别老挝"AND类别2-"的帖子4Days"已附上.

我尝试使用 Query Multiple Taxonomies 插件.然而,这种堵塞是检索所有老挝"类别的帖子和所有停留时间为2-4天"的帖子.

我知道,我可以使用此查询过滤帖子,但是,我需要一些帮助才能使用提交按钮创建此小部件.另外,我想对其进行自定义以删除父类别并将它们显示为标题(删除国家/地区"和逗留时间"复选框并为其添加特定类别)?

工作查询:

<?php//cat 42=老挝猫 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="永久链接<?php the_title();?>"><?php the_title(); ?><?php the_excerpt(); ?></a><?php endwhile;?>

感谢您的帮助

我做了什么:

  1. 我创建了一个新的侧边栏,我想在其中添加新表单
  2. 我创建了这个新侧边栏的模板sidebar-filters.php".这是我在里面的代码.

  3. <input type="hidden" name="submitted" id="submitted" value="true"/></表单>

  4. 这是我在 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 (!empty($arr) && in_array($value, $arr)) $checked = 'checked';返回 $checked;}

function my_query($query){//你可以在这里使用 is_archive() 或任何你需要的东西如果 ($query->is_main_query() && is_search()) {$cat_and = array();foreach (array('countries', 'stays') as $variable) {$categories = get_query_var($variable);如果 (!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');但是,我暂时什么也没有检索,我做错了什么?

解决方案

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

首先,您必须注册新的查询变量,以便您可以使用 GET 并在 url 中设置过滤器(您可以稍后根据需要重写):

add_filter('query_vars', 'my_query_vars');函数 my_query_vars($vars){array_push($vars, 'countries', 'stays');返回 $vars;}

然后创建一个表单并使用每组复选框的数组名称打印类别.您需要确保恢复提交表单后选中的复选框,您可以在同一页面上执行此操作.

function set_checked($arr, $value){$checked = '';if (!empty($arr) && in_array($value, $arr)) $checked = 'checked';返回 $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');//开始表格foreach ($countries 作为 $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));}//结束形式

最后您需要修改查询以根据此变量进行过滤:

add_action('pre_get_posts', 'my_query');函数 my_query($query){//你可以在这里使用 is_archive() 或任何你需要的东西如果 ($query->is_main_query() && is_search()) {$cat_and = array();foreach (array('countries', 'stays') as $variable) {$categories = get_query_var($variable);如果 (!empty($categories)) {$cat_and = array_merge($cat_and, $categories);}}if (!empty($cat_and)) $query->set('category__and', $cat_and);}}

我还没有测试过,但应该可以,希望对您有所帮助.

I would like to set up a widget to filter my post by categories. Let's say, I do have two different categories, "Countries" and "length of stay" with sub categories. Here is an example of what I have:

What I want, is to filter posts by multiple categories. So, If the user is checking the country "Laos" and a length of stay of "2-4Days", I would like to retrieve only posts where the category "Laos" AND the category "2-4Days" has been attached.

I tried to use the Query Multiple Taxonomies pluging. However, this plugging is retrieving all posts with the "Laos" category and all posts with the length of stay of "2-4Days".

I know, that I can filter post with this query, but, I need some help to create this widget with the submit button. Also, I would like to customise it to remove parent categories and display them as a title (remove the checkbox of "Countries" and "Length of stay" and add to them a specific class)?

Working query:

<?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; ?>

Thanks for helping

What I have done :

  1. I create a new side-bar where I want to add the new form
  2. I create the template "sidebar-filters.php" of this new side bar. Here is the code That I had inside it.

    <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. And here is the code that I had in my function.php

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

function set_checked($arr, $value) { $checked = ''; if (!empty($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?

解决方案

Here's the basic idea to do this with a form:

First you would have to register new query vars so you can use GET and have the filters in the url (that you can later rewrite if needed):

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

Then create a form and print the categories using an array name for each group of checkboxes. You need to make sure to restore the checkboxes that were checked after submitting the form, you can do this on the same page.

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

Finally you need to modify the query to filter according to this variables:

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);
  }
}

I haven't tested it but this should work, hope this helps.

这篇关于按多个复选框类别过滤帖子 - wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆