Loading
0

wordpress 通过自定义分类法taxonomy多重筛选文章

之前有个客户找模板兔做wordpress多重筛选功能,主要通过自定义分类法来筛选。下面给出大致的代码片段,供有一定开发基础的人看,若不懂代码,可联系模板兔开发。

处理过滤的代码:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array(
 'post_type' => 'post',
 'paged' => $paged
 );

$args['tax_query'] = array();

if(isset($_GET['city']) && $_GET['city'] > 0){
 array_push($args['tax_query'], array('taxonomy' => 'city','field' => 'term_id','terms' => $_GET['city']) );
 }
 if(isset($_GET['function']) && $_GET['function'] > 0){
 array_push($args['tax_query'], array('taxonomy' => 'function','field' => 'term_id','terms' => $_GET['function']) );
 }
 if(isset($_GET['industry']) && $_GET['industry'] > 0){
 array_push($args['tax_query'], array('taxonomy' => 'industry','field' => 'term_id','terms' => $_GET['industry']) );
 }
 if(isset($_GET['scene']) && $_GET['scene'] > 0){
 array_push($args['tax_query'], array('taxonomy' => 'scene','field' => 'term_id','terms' => $_GET['scene']) );
 }
 if(isset($_GET['price']) && $_GET['price'] > 0){
 array_push($args['tax_query'], array('taxonomy' => 'price','field' => 'term_id','terms' => $_GET['price']) );
 }


 query_posts($args);
 if (have_posts()) : while (have_posts()) : the_post();

 endwhile;endif;?>

获取过滤链接的函数

function getFilterUrl($city,$function,$industry,$price,$scene=0){
 $filter = '?filter=1';
 if( $city ){
 $filter .= '&city='.$city;
 }else{
 $filter .= '&city=0';
 }
 if( $function ){
 $filter .= '&function='.$function;
 }else{
 $filter .= '&function=0';
 }
 if($scene){
 if( $industry ){
 $filter .= '&scene='.$industry;
 }else{
 $filter .= '&scene=0';
 }
 }else{
 if( $industry ){
 $filter .= '&industry='.$industry;
 }else{
 $filter .= '&industry=0';
 }
 }
 if( $price ){
 $filter .= '&price='.$price;
 }else{
 $filter .= '&price=0';
 }
 return $filter;
 }

另附一个案例核心代码下载:https://pan.baidu.com/s/1slfXikH 密码: g8c9

特别声明:1、本站仅提供源码学习下载,使用者需具备一定的技术基础,源码费用仅为站长辛苦整理费,不代表源码自身价值也不包含任何服务,如需完美运营请到官方购买。如需搭建、二开、bug修复等服务需额外收费,如果源码侵犯了您的利益请联系客服处理! 2、用户必须遵守《计算机软件保护条例(2013修订)》第十七条:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。鉴于此条例,用户从本平台下载的全部源码(软件)仅限学习研究,未经版权归属者授权不得商用,若因商用引起的版权纠纷,一切责任均由使用者自行承担,本平台所属公司及其雇员不承担任何法律责任。本站邮箱:weituiw@qq.com