PHP-按多个复选框类别过滤帖子-WordPress
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-按多个复选框类别过滤帖子-WordPress,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4817字,纯文字阅读大概需要7分钟。
内容图文
![PHP-按多个复选框类别过滤帖子-WordPress](/upload/InfoBanner/zyjiaocheng/677/8ac0fc5f35e14b9492045efb2a3100ef.jpg)
我想设置一个小部件以按类别过滤我的帖子.
比方说,我确实有两个不同的类别,带有子类别的“国家”和“逗留时间”.这是我所拥有的一个例子:
我想要的是按多个类别过滤帖子.因此,如果用户正在检查国家“老挝”且停留时间为“ 2-4天”,我只想检索已附加了“老挝”类别和“ 2-4天”类别的帖子.
我尝试使用Query Multiple Taxonomies插入.但是,这种堵塞是
检索“老挝”类别的所有帖子以及停留时间为“ 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($vars)
{
array_push($vars,’countries’,’stays’);
返回$vars;
}
add_filter(‘query_vars’,’my_query_vars’);
函数set_checked($arr,$value)
{
$checked =”;
如果(!empty($arr)&& in_array($value,$arr))$checked =’checked’;
返回$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?
解决方法:
这是使用表单执行此操作的基本思想:
首先,您必须注册新的查询变量,以便可以使用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);
}
}
我还没有测试过,但是应该可以,希望对您有所帮助.
内容总结
以上是互联网集市为您收集整理的PHP-按多个复选框类别过滤帖子-WordPress全部内容,希望文章能够帮你解决PHP-按多个复选框类别过滤帖子-WordPress所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。