简体   繁体   English

WordPress query_posts类别下拉过滤器

[英]Wordpress query_posts category dropdown filter

I'm develeoping a dropdown menu for wordpress that is filtering diferent categories from posts. 我正在为wordpress设计一个下拉菜单,该菜单从帖子中过滤出不同的类别。 I'm currently using query_posts function like this: 我目前正在使用query_posts函数,如下所示:

query_posts( array('category__and'=>array($_GET['operation'],$_GET['type'])));

The get $_GET['operation'] and $_GET['type'] are passed obviously by get params from the form dropdown menu. get $_GET['operation']$_GET['type']显然是通过表单下拉菜单中的get参数传递的。

When I pass the 2 values throught the form the query runs correctly, it displays the posts within the correct categories selected, all good. 当我将2个值传递给查询正确运行的表单时,它将显示所选正确类别内的帖子,一切都很好。

The trouble comes when at the form I'm not defining any one of the get values, so the get from the url is like empty. 当我没有定义任何get值的形式时,麻烦就来了,所以从url获得的结果就像是空的。

Example:
operation=4
type=2

It Runs correctly. 它可以正确运行。

Trouble:
operation=""
type=2

The query or whatever i can't see breaks and displays that there are no results. 查询或任何我看不到的中断,并显示没有结果。

I hope if there is any way to check if any value is empty and exclude it from the array? 我希望是否有任何方法可以检查任何值是否为空并将其从数组中排除? Anything like: 任何类似:

query_posts( array('category__and'=>array(
if($_GET['operation']!=""){
$_GET['operation'],
}
$_GET['type']

))
);

Please help! 请帮忙!

Something like this should do what you need. 这样的事情应该可以满足您的需求。

$args = array();

if (!empty($_GET['operation']))
  $args[] = $_GET['operation'];

if (!empty($_GET['type']))
  $args[] = $_GET['type'];

if (!empty($args))
  query_posts( array('category__and'=>$args));
else
  query_posts();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM