本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
之前有个客户找魔扣科技做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;
- }
复制代码
|