按多个复选框类别过滤帖子 - wordpress [英] Filter posts by multiple checkbox categories - 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;?>
感谢您的帮助
我做了什么:
- 我创建了一个新的侧边栏,我想在其中添加新表单
我创建了这个新侧边栏的模板sidebar-filters.php".这是我在里面的代码.
我是侧边栏过滤器<?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" 方法="post"><ul><li><label><b>国家</b></label></li><br/><?phpforeach ($countries 作为 $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>住院时间</b></label></li><br/><?phpforeach ($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">发送电子邮件</button><input type="hidden" name="submitted" id="submitted" value="true"/></表单>这是我在 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 :
- I create a new side-bar where I want to add the new form
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>
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屋!
查看全文
相关文章
- WordPress - 生成按标签和类别过滤的帖子列表;
- 创建if语句以按Wordpress中的cpt按类别过滤帖子;
- 按类别过滤 Wordpress 帖子时对 GatsbyJS 页面进行分页;
- WordPress-产生按标签和类别过滤的帖子列表;
- 按类别过滤Wordpress帖子时对GatsbyJS页面进行分页;
- Magento 按多个类别过滤产品集合;
- jQuery多个复选框过滤;
- 按类别过滤博客/连接Web部件;
- Magento的 - 获得按类别过滤属性;
- Facebook图形API按类别过滤Feed;
- Prestashop 使用 WebService 按类别过滤产品;
- 按类别过滤JSON结果-React Hooks;
- Wordpress API 按类别获取帖子;
- 按子类别显示 wordpress 帖子;
- 在angularJS多个复选框过滤;
- 多个复选框的Jquery过滤;
- jQuery DataTables:多个复选框过滤;
- 多个复选框过滤器;
- 如何按类别,值和已检查复选框对复选框进行排序;
- 使用AngularJS中的复选框按多个条件过滤数据;
- Magento ->addCategoryFilter - 按根类别过滤产品集合;
- WordPress按标签和类别查询帖子;
- 按类别和标签列出wordpress帖子;
- 过滤多个复选框的html表;
- 使用jQuery的多个复选框过滤;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;