代码实现WordPress 文章多条件多重字段文章筛选功能

最近在针对逍遥乐的模板的时候需要用到多条件多重字段文章,网上各种分类法、标签法等五花八门,甚至有些还需要安装插件,很不方便!逍遥今天给大家带来的是由模板兔写的多重字段多条件功能代码。

效果如下:(这里只列出3项自定义字段)

代码实现WordPress 文章多条件多重字段文章筛选功能

筛选界面代码如下:

<div class="filters">
 <div class="filter-item">
 数据标价:<a href="<?php echo add_query_arg("price","all",selfURL())?>" class="<?php if(!isset($_GET['price']) || $_GET['price'] == 'all') echo 'on';?>">全部</a> <a href="<?php echo add_query_arg("price","free",selfURL())?>" class="<?php if($_GET['price'] == 'free') echo 'on';?>">免费</a> <a href="<?php echo add_query_arg("price","fee",selfURL())?>" class="<?php if($_GET['price'] == 'fee') echo 'on';?>">付费</a>
 </div>
 <div class="filter-item">
 数据来源:<a href="<?php echo add_query_arg("from","all",selfURL())?>" class="<?php if(!isset($_GET['from']) || $_GET['from'] == 'all') echo 'on';?>">全部</a> <a href="<?php echo add_query_arg("from","data",selfURL())?>" class="<?php if($_GET['from'] == 'data') echo 'on';?>">引力数据</a> <a href="<?php echo add_query_arg("from","organization",selfURL())?>" class="<?php if($_GET['from'] == 'organization') echo 'on';?>">机构</a> <a href="<?php echo add_query_arg("from","internet",selfURL())?>" class="<?php if($_GET['from'] == 'internet') echo 'on';?>">互联网</a> <a href="<?php echo add_query_arg("from","personal",selfURL())?>" class="<?php if($_GET['from'] == 'personal') echo 'on';?>">个人</a>
 </div>
 <div class="filter-item">
 数据大小:<a href="<?php echo add_query_arg("size","0",selfURL())?>" class="<?php if(!isset($_GET['size']) || $_GET['size'] == '0') echo 'on';?>">全部</a> <a href="<?php echo add_query_arg("size","1",selfURL())?>" class="<?php if($_GET['size'] == '1') echo 'on';?>">100M以下</a> <a href="<?php echo add_query_arg("size","2",selfURL())?>" class="<?php if($_GET['size'] == '2') echo 'on';?>">100M-500M</a> <a href="<?php echo add_query_arg("size","3",selfURL())?>" class="<?php if($_GET['size'] == '3') echo 'on';?>">500M-1G</a> <a href="<?php echo add_query_arg("size","4",selfURL())?>" class="<?php if($_GET['size'] == '4') echo 'on';?>">1G以上</a>
 </div>
 </div>

筛选功能部分代码如下:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $metaArray = array(); //自定义字段数组by mobantu

if(isset($_GET['price'])){
 if($_GET['price'] == 'free'){
 $array_temp = array('key' => 'down_price', 'value'=>'0', 'compare'=>'=');
 array_push($metaArray,$array_temp);
 }elseif($_GET['price'] == 'fee'){
 $array_temp = array('key' => 'down_price', 'value'=>'0', 'compare'=>'>');
 array_push($metaArray,$array_temp);
 }
 }

if(isset($_GET['from'])){
 $array_temp = array('key' => 'down_from', 'value'=>$_GET['from'], 'compare'=>'=');
 array_push($metaArray,$array_temp);
 }

if(isset($_GET['size'])){
 if($_GET['size'] == '1'){
 $array_temp = array('key' => 'down_size', 'value'=>'100', 'type' => 'numeric', 'compare'=>'<'); 
 array_push($metaArray,$array_temp);
 }if($_GET['size'] == '2'){
 $array_temp = array('key' => 'down_size', 'value'=>array( 100, 500 ), 'type' => 'numeric', 'compare'=>'BETWEEN'); 
 array_push($metaArray,$array_temp);
 }
 }


 $args = array(
 'order' => DESC,
 'caller_get_posts' => 1,
 'meta_query' => $metaArray,
 'paged' => $paged
 );

query_posts($args);

看了我的代码,相信你脑洞大开,妈妈再也不用担心你的了~~

 

另外,之前也给另一个客户基于Mohtml主题开发了一个评分的功能,弄了一个评分筛选小工具,小工具选项:

代码实现WordPress 文章多条件多重字段文章筛选功能

部分代码如下,没有上面的代码方便,不过也可以参考:

$order = "DESC";
 $orderby = 'date';
 $meta_key = '';

if($review_order == 'review_desc'){
 $orderby = 'meta_value_num';
 $meta_key = 'review_'.$show_style;

}elseif($review_order == 'review_asc'){
 $order = 'ASC';
 $orderby = 'meta_value_num';
 $meta_key = 'review_'.$show_style;

}elseif($review_order == 'date_asc'){
 $order = 'ASC';
 }elseif($review_order == 'random'){
 $orderby = 'rand';
 }

if($show_date == '7'){
 $args = array(
 'order' => $order,
 'orderby' => $orderby,
 'meta_key' => $meta_key,
 'meta_query' => array(
 array('key'=>'review_type','value'=>$type, 'compare'=>'IN'),
 ),
 'date_query' => array(
 array(
 'after' => '1 week ago'
 )
 ),
 'showposts' => $limit,
 'caller_get_posts' => 1
 );
 }elseif($show_date == '30'){
 $args = array(
 'order' => $order,
 'orderby' => $orderby,
 'meta_key' => $meta_key,
 'meta_query' => array(
 array('key'=>'review_type','value'=>$type, 'compare'=>'IN'),
 ),
 'date_query' => array(
 array(
 'after' => '1 month ago'
 )
 ),
 'showposts' => $limit,
 'caller_get_posts' => 1
 );
 }elseif($show_date == '183'){
 $args = array(
 'order' => $order,
 'orderby' => $orderby,
 'meta_key' => $meta_key,
 'meta_query' => array(
 array('key'=>'review_type','value'=>$type, 'compare'=>'IN'),
 ),
 'date_query' => array(
 array(
 'after' => '6 month ago'
 )
 ),
 'showposts' => $limit,
 'caller_get_posts' => 1
 );
 }elseif($show_date == '30'){
 $args = array(
 'order' => $order,
 'orderby' => $orderby,
 'meta_key' => $meta_key,
 'meta_query' => array(
 array('key'=>'review_type','value'=>$type, 'compare'=>'IN'),
 ),
 'date_query' => array(
 array(
 'after' => '1 year ago'
 )
 ),
 'showposts' => $limit,
 'caller_get_posts' => 1
 );
 }else{
 $args = array(
 'order' => $order,
 'orderby' => $orderby,
 'meta_key' => $meta_key,
 'meta_query' => array(
 array('key'=>'review_type','value'=>$type, 'compare'=>'IN'),
 ),
 'showposts' => $limit,
 'caller_get_posts' => 1
 );
 }
 query_posts($args);

无限的学习!