wordpress进阶教程(四十):文章筛选功能


前几天一网友找我写一个筛选功能,他要做一个关于房产信息的网站,希望访客能在网页上根据条件筛选符合条件的文章。

根据他的功能要求,我给他的建议是,给每个筛选条件建立自定义分类法。比如他需求是根据四个条件来筛选,省、市、类型、价格。

其次,筛选页面我还是根据习惯使用了以前的伪静态方法,使访问的url地址类型为:www.shouce.ren/sift/、www.shouce.ren/sift/0_0_0_0、www.shouce.ren/sift/1_23_33_32/  后面以下划线分割的字符,就是我们要筛选的参数,分别为四个分类的ID。然后根据这四个ID值,来筛选符合条件的文章。最终效果:sift

要做这种功能,内容较多,文章最后面直接提供一个我已经写好了此功能的极简单主题,安装即可用。

注意:本文内容仅仅在伪静态状态下可用,win主机,不管你伪不伪静态,,,都勿用。

步骤一:建立自定义分类法。

本教程讲述的内容中用到了四个筛选条件:省、市、类型、价格。所以,我就给建立四个自定义分类法(使用默认的post)。使用下述代码,你可以直接复制粘贴到functions.php文件中。关于自定义分类法,你也可以访问wordpress进阶教程(三):创建自定义分类法

下列代码存放在本教程提供的主题中的 include/taxonomy.php文件中

  1. //给post创建四个自定义分类法
  2. add_action('init', 'ashu_post_type');
  3. function ashu_post_type() {
  4.   register_taxonomy(
  5.     'province',
  6.     'post',
  7.     array(
  8.       'label' => '省',
  9.       'rewrite' => array( 'slug' => 'province' ),
  10.       'hierarchical' => true
  11.     )
  12.   );
  13.   register_taxonomy(
  14.     'city',
  15.     'post',
  16.     array(
  17.       'label' => '市',
  18.       'rewrite' => array( 'slug' => 'city' ),
  19.       'hierarchical' => true
  20.     )
  21.   );
  22.   register_taxonomy(
  23.     'genre',
  24.     'post',
  25.     array(
  26.       'label' => '类型',
  27.       'rewrite' => array( 'slug' => 'genre' ),
  28.       'hierarchical' => true
  29.     )
  30.   );
  31.   register_taxonomy(
  32.     'price',
  33.     'post',
  34.     array(
  35.       'label' => '价格',
  36.       'rewrite' => array( 'slug' => 'price' ),
  37.       'hierarchical' => true
  38.     )
  39.   );
  40. }
  41. //获取筛选页面的Url
  42. function ashuwp_sift_link(){
  43.   return home_url()."/sift";
  44. }
  45. /*
  46. *添加query变量
  47. */
  48. function ashuwp_query_vars($public_query_vars) {
  49.     $public_query_vars[] = 'ashuwp_page';
  50.     $public_query_vars[] = 'condition';
  51.     return $public_query_vars;
  52. }
  53. /*
  54. *sift页面的重写规则,三种url:
  55. *shouce.ren/sift   shouce.ren/sift/0_0_0_0/    shouce.ren/sift/0_0_0_0/page/2
  56. */
  57. function ashuwp_rewrite_rules( $wp_rewrite ){
  58.   $new_rules = array(
  59.     'sift/?$' => 'index.php?ashuwp_page=sift',
  60.     'sift/([^/]+)/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1),
  61.     'sift/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2)
  62.   );
  63.   $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  64. }
  65. /*
  66. *载入模板规则
  67. *用page-sift.php作为筛选页面的模板文件
  68. */
  69. function ashuwp_template_redirect(){
  70.   global $wp,$wp_query,$wp_rewrite;
  71.   if( !isset($wp_query->query_vars['ashuwp_page']) )
  72.     return;
  73.   $reditect_page =  $wp_query->query_vars['ashuwp_page'];
  74.   if ($reditect_page == "sift"){
  75.     include(get_template_directory().'/page-sift.php');
  76.     die();
  77.   }
  78. }
  79. /*
  80. *更新重写规则
  81. *激活主题的时候
  82. */
  83. function ashuwp_flush_rewrite_rules() {
  84.   global $pagenow, $wp_rewrite;
  85.   if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
  86.     $wp_rewrite->flush_rules();
  87. }
  88. add_action( 'load-themes.php', 'ashuwp_flush_rewrite_rules' );
  89. add_action('generate_rewrite_rules', 'ashuwp_rewrite_rules' );
  90. add_action('query_vars', 'ashuwp_query_vars');
  91. add_action("template_redirect", 'ashuwp_template_redirect');
  92. 筛选页面的内容较多,直接贴出,部分代码有注释。大概思路就是:
  93. 1. 先获取所有分类法的所有分类ID,有两个用途:判断从url获取的分类ID是否存在、输出筛选页面中筛选条件的url
  94. 2.url中获取四个ID参数。
  95. 3.输出筛选条件列表。注意判断当前访问、以及“不限”的条件
  96. 4.根据条件查询文章。
  97. 下面代码存在放本教程提供的主题的page-sift.php文件中
  98. <!DOCTYPE html>
  99. <html lang="zh-CN">
  100. <head>
  101. <meta charset="UTF-8">
  102. <title>阿树工作室--筛选页面教程</title>
  103. <link rel='stylesheet' id='ashuwp-style-css'  href='<?php echo get_stylesheet_uri(); ?>' type='text/css' media='all' />
  104. </head>
  105. <body>
  106. <div id="site-page">
  107. <div id="header">
  108. <h1 id="logo">
  109.  <a href="http://www.treework.cn">treework.cn</a>
  110. </h1>
  111. <h2 class="align-cenetr">阿树工作室--筛选页面教程</h2>
  112. </div>
  113. <div class="container content">
  114. <?php
  115. //1.1 获取所有province分类,将id放入 $province_id数组
  116. $args = array(
  117.   'taxonomy'=>'province',
  118.   'orderby'=>'id',
  119.   'hide_empty'=>0
  120. );
  121. $province_ob = get_categories( $args );
  122. $province_id = array();
  123. foreach($province_ob as $province){
  124.   $province_id[] = $province->term_id;
  125. }
  126. //1.2 获取所有city分类,将id放入 $city_id数组
  127. $args = array(
  128.   'taxonomy'=>'city',
  129.   'orderby'=>'id',
  130.   'hide_empty'=>0
  131. );
  132. $city_ob = get_categories( $args );
  133. $city_id = array();
  134. foreach($city_ob as $city){
  135.   $city_id[] = $city->term_id;
  136. }
  137. //1.3 获取所有genre分类,将id放入 $genre_id数组
  138. $args = array(
  139.   'taxonomy'=>'genre',
  140.   'orderby'=>'id',
  141.   'hide_empty'=>0
  142. );
  143. $genre_ob = get_categories( $args );
  144. $genre_id = array();
  145. foreach($genre_ob as $genre){
  146.   $genre_id[] = $genre->term_id;
  147. }
  148. //1.4 获取所有price分类,将id放入 $price_id数组
  149. $args = array(
  150.   'taxonomy'=>'price',
  151.   'orderby'=>'id',
  152.   'hide_empty'=>0
  153. );
  154. $price_ob = get_categories( $args );
  155. $price_id = array();
  156. foreach($price_ob as $price){
  157.   $price_id[] = $price->term_id;
  158. }
  159. //2 参数处理
  160. //2.1 页码
  161. $wp_query->query_vars['paged'] > 1 ? $pagenum = $wp_query->query_vars['paged'] : $pagenum = 1;
  162. /*2.2 从url中获取参数 即url中 0_0_0_0
  163. *将获取到的四个参数放入 $cons 数组中
  164. */
  165. global $wp_query;
  166. if( isset($wp_query->query_vars['condition']) && $wp_query->query_vars['condition']!='' ){
  167.   $condition = $wp_query->query_vars['condition'];
  168.   $conditions = explode('_',$condition);
  169.   $cons = array();
  170.   if(isset($conditions[0])){
  171.     $conditions[0] = (int)$conditions[0];
  172.   }else{
  173.     $conditions[0]=0;
  174.   }
  175.   if(isset($conditions[1])){
  176.     $conditions[1] = (int)$conditions[1];
  177.   }else{
  178.     $conditions[1]=0;
  179.   }
  180.   if(isset($conditions[2])){
  181.     $conditions[2] = (int)$conditions[2];
  182.   }else{
  183.     $conditions[2]=0;
  184.   }
  185.   if(isset($conditions[3])){
  186.     $conditions[3] = (int)$conditions[3];
  187.   }else{
  188.     $conditions[3]=0;
  189.   }
  190.   //从url中获取到的各分类法分类ID是否真实存在
  191.   if( in_array($conditions[0],$province_id) ){
  192.     $cons[0]=$conditions[0];
  193.   }else{
  194.     $cons[0]=0;
  195.   }
  196.   if( in_array($conditions[1],$city_id) ){
  197.     $cons[1]=$conditions[1];
  198.   }else{
  199.     $cons[1]=0;
  200.   }
  201.   if( in_array($conditions[2],$genre_id) ){
  202.     $cons[2]=$conditions[2];
  203.   }else{
  204.     $cons[2]=0;
  205.   }
  206.   if( in_array($conditions[3],$price_id) ){
  207.     $cons[3]=$conditions[3];
  208.   }else{
  209.     $cons[3]=0;
  210.   }
  211.   $sift_link = ashuwp_sift_link().'/'.$cons[0].'_'.$cons[1].'_'.$cons[2].'_'.$cons[3];
  212. }else{
  213.   $cons = array(0,0,0,0);
  214.   $sift_link = ashuwp_sift_link().'/0_0_0_0';
  215. }
  216. ?>
  217. <div class="sift_query">
  218. <div class="sift_cons">
  219. <div class="sift_li">
  220. <span>省:</span>
  221. <<?php if($cons[0]==0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/0_<?php echo $cons[1];?>_<?php echo $cons[2];?>_<?php echo $cons[3];?>/">不限</a>
  222. <?php
  223. foreach( $province_ob as $province ){
  224. ?>
  225.   <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $province->term_id; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[0] == $province->term_id){ echo 'class="current"'; } ?>><?php echo $province->name; ?></a>
  226. <?php } ?>
  227. </div>
  228. <div class="sift_li"><span>市:</span><a <?php if($cons[1] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_0_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>/">不限</a>
  229. <?php
  230. foreach( $city_ob as $city ){ ?>
  231.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $city->term_id; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[1] == $city->term_id){ echo 'class="current"'; } ?>><?php echo $city->name; ?></a>
  232. <?php } ?>
  233. </div>
  234. <div class="sift_li"><span>类型:</span><a <?php if($cons[2] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_0_<?php echo $cons[3];?>/">不限</a>
  235. <?php
  236. foreach( $genre_ob as $genre ){ ?>
  237.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $genre->term_id; ?>_<?php echo $cons[3];?>" <?php if($cons[2] == $genre->term_id){ echo 'class="current"'; } ?>><?php echo $genre->name; ?></a>
  238. <?php } ?>
  239. </div>
  240. <div class="sift_li"><span>价格:</span><a <?php if($cons[3] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_0/">不限</a>
  241. <?php
  242. foreach( $price_ob as $price ){ ?>
  243.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $price->term_id; ?>" <?php if($cons[3] == $price->term_id){ echo 'class="current"'; } ?>><?php echo $price->name; ?></a>
  244. <?php } ?>
  245. </div>
  246. </div>
  247. <?php
  248. //将获取到的参数组合为query_posts的参数
  249. $tax_query = array(
  250.     'relation'=> 'AND',
  251. );
  252. //province
  253. if( $cons[0] != 0 ){
  254.     $tax_query[] = array(
  255.         'taxonomy'=>'province',
  256.         'field'=>'id',
  257.         'terms'=>$cons[0]
  258.     );
  259. }
  260. //city
  261. if( $cons[1] != 0 ){
  262.     $tax_query[] = array(
  263.         'taxonomy'=>'city',
  264.         'field'=>'id',
  265.         'terms'=>$cons[1]
  266.     );
  267. }
  268. //genre
  269. if( $cons[2] != 0 ){
  270.     $tax_query[] = array(
  271.         'taxonomy'=>'genre',
  272.         'field'=>'id',
  273.         'terms'=>$cons[2]
  274.     );
  275. }
  276. //price
  277. if( $cons[3] != 0 ){
  278.     $tax_query[] = array(
  279.         'taxonomy'=>'price',
  280.         'field'=>'id',
  281.         'terms'=>$cons[3]
  282.     );
  283. }
  284. $args = array(
  285.     'paged' => $pagenum,
  286.     'tax_query'=> $tax_query
  287. );
  288. global $ashuwp_query;
  289. $ashuwp_query = new WP_Query( $args );
  290. ?>
  291. <div class="query_count">共找到<?php echo $ashuwp_query->found_posts;?>个符合条件的内容</div>
  292. </div>
  293. <?php
  294. if($ashuwp_query->have_posts()) : ?>
  295. <div id="post_list">
  296. <?php while($ashuwp_query->have_posts()) : $ashuwp_query->the_post(); ?>
  297. <div class="post">
  298.   <a href="<?php the_permalink();?>"><?php the_title();?></a>
  299. </div>
  300. <?php endwhile;?>
  301. </div>
  302. <?php endif; ?>
  303. <div id="ashuwp_page">
  304. <?php
  305. $pagination = paginate_links( array(
  306.   'base' => $links.'/page/%#%',
  307.   'format' => '/page/%#%',
  308.   'prev_text' => '上一页',
  309.   'next_text' => '下一页',
  310.   'total' => $ashuwp_query->max_num_pages,
  311.   'current' => $pagenum
  312. ) );
  313. if ( $pagination ) {
  314.     echo $pagination;
  315. }
  316. ?>
  317. </div>
  318. </div>
  319. </div><!--site-page-->
  320. <div id="footer">
  321. <div class="container">
  322. <p>阿树工作室,联系作者admin@treework.cn</p>
  323. </div>
  324. </div>
  325. </body>
  326. </html>

添加完代码,应该在后台文章下面会多出四个自定义分类法:taxonomy

步骤二:添加筛选默认,重写规则。

1. 先在主题文件夹下面建立一个page-sift.php文件(文件名无要求,与下面代码中对应即可),这个文件将作为筛选页面的模板文件。

2.添加重写规则,使得用户访问类似www.shouce.ren/sift、www.shouce.ren/sift/0_0_0_0类型url时,不会出现404,而且能正确加载模板。

关于wordpress重写规则,你也可以访问wordpress进阶教程(16):添加一个重写规则,构建新页面初试

下列代码存放在本教程提供的主题中的 include/rewrite.php文件中:

  1. //给post创建四个自定义分类法
  2. add_action('init', 'ashu_post_type');
  3. function ashu_post_type() {
  4.   register_taxonomy(
  5.     'province',
  6.     'post',
  7.     array(
  8.       'label' => '省',
  9.       'rewrite' => array( 'slug' => 'province' ),
  10.       'hierarchical' => true
  11.     )
  12.   );
  13.   register_taxonomy(
  14.     'city',
  15.     'post',
  16.     array(
  17.       'label' => '市',
  18.       'rewrite' => array( 'slug' => 'city' ),
  19.       'hierarchical' => true
  20.     )
  21.   );
  22.   register_taxonomy(
  23.     'genre',
  24.     'post',
  25.     array(
  26.       'label' => '类型',
  27.       'rewrite' => array( 'slug' => 'genre' ),
  28.       'hierarchical' => true
  29.     )
  30.   );
  31.   register_taxonomy(
  32.     'price',
  33.     'post',
  34.     array(
  35.       'label' => '价格',
  36.       'rewrite' => array( 'slug' => 'price' ),
  37.       'hierarchical' => true
  38.     )
  39.   );
  40. }
  41. //获取筛选页面的Url
  42. function ashuwp_sift_link(){
  43.   return home_url()."/sift";
  44. }
  45. /*
  46. *添加query变量
  47. */
  48. function ashuwp_query_vars($public_query_vars) {
  49.     $public_query_vars[] = 'ashuwp_page';
  50.     $public_query_vars[] = 'condition';
  51.     return $public_query_vars;
  52. }
  53. /*
  54. *sift页面的重写规则,三种url:
  55. *shouce.ren/sift   shouce.ren/sift/0_0_0_0/    shouce.ren/sift/0_0_0_0/page/2
  56. */
  57. function ashuwp_rewrite_rules( $wp_rewrite ){
  58.   $new_rules = array(
  59.     'sift/?$' => 'index.php?ashuwp_page=sift',
  60.     'sift/([^/]+)/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1),
  61.     'sift/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2)
  62.   );
  63.   $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  64. }
  65. /*
  66. *载入模板规则
  67. *用page-sift.php作为筛选页面的模板文件
  68. */
  69. function ashuwp_template_redirect(){
  70.   global $wp,$wp_query,$wp_rewrite;
  71.   if( !isset($wp_query->query_vars['ashuwp_page']) )
  72.     return;
  73.   $reditect_page =  $wp_query->query_vars['ashuwp_page'];
  74.   if ($reditect_page == "sift"){
  75.     include(get_template_directory().'/page-sift.php');
  76.     die();
  77.   }
  78. }
  79. /*
  80. *更新重写规则
  81. *激活主题的时候
  82. */
  83. function ashuwp_flush_rewrite_rules() {
  84.   global $pagenow, $wp_rewrite;
  85.   if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
  86.     $wp_rewrite->flush_rules();
  87. }
  88. add_action( 'load-themes.php', 'ashuwp_flush_rewrite_rules' );
  89. add_action('generate_rewrite_rules', 'ashuwp_rewrite_rules' );
  90. add_action('query_vars', 'ashuwp_query_vars');
  91. add_action("template_redirect", 'ashuwp_template_redirect');
  92. 筛选页面的内容较多,直接贴出,部分代码有注释。大概思路就是:
  93. 1. 先获取所有分类法的所有分类ID,有两个用途:判断从url获取的分类ID是否存在、输出筛选页面中筛选条件的url
  94. 2.url中获取四个ID参数。
  95. 3.输出筛选条件列表。注意判断当前访问、以及“不限”的条件
  96. 4.根据条件查询文章。
  97. 下面代码存在放本教程提供的主题的page-sift.php文件中
  98. <!DOCTYPE html>
  99. <html lang="zh-CN">
  100. <head>
  101. <meta charset="UTF-8">
  102. <title>阿树工作室--筛选页面教程</title>
  103. <link rel='stylesheet' id='ashuwp-style-css'  href='<?php echo get_stylesheet_uri(); ?>' type='text/css' media='all' />
  104. </head>
  105. <body>
  106. <div id="site-page">
  107. <div id="header">
  108. <h1 id="logo">
  109.  <a href="http://www.treework.cn">treework.cn</a>
  110. </h1>
  111. <h2 class="align-cenetr">阿树工作室--筛选页面教程</h2>
  112. </div>
  113. <div class="container content">
  114. <?php
  115. //1.1 获取所有province分类,将id放入 $province_id数组
  116. $args = array(
  117.   'taxonomy'=>'province',
  118.   'orderby'=>'id',
  119.   'hide_empty'=>0
  120. );
  121. $province_ob = get_categories( $args );
  122. $province_id = array();
  123. foreach($province_ob as $province){
  124.   $province_id[] = $province->term_id;
  125. }
  126. //1.2 获取所有city分类,将id放入 $city_id数组
  127. $args = array(
  128.   'taxonomy'=>'city',
  129.   'orderby'=>'id',
  130.   'hide_empty'=>0
  131. );
  132. $city_ob = get_categories( $args );
  133. $city_id = array();
  134. foreach($city_ob as $city){
  135.   $city_id[] = $city->term_id;
  136. }
  137. //1.3 获取所有genre分类,将id放入 $genre_id数组
  138. $args = array(
  139.   'taxonomy'=>'genre',
  140.   'orderby'=>'id',
  141.   'hide_empty'=>0
  142. );
  143. $genre_ob = get_categories( $args );
  144. $genre_id = array();
  145. foreach($genre_ob as $genre){
  146.   $genre_id[] = $genre->term_id;
  147. }
  148. //1.4 获取所有price分类,将id放入 $price_id数组
  149. $args = array(
  150.   'taxonomy'=>'price',
  151.   'orderby'=>'id',
  152.   'hide_empty'=>0
  153. );
  154. $price_ob = get_categories( $args );
  155. $price_id = array();
  156. foreach($price_ob as $price){
  157.   $price_id[] = $price->term_id;
  158. }
  159. //2 参数处理
  160. //2.1 页码
  161. $wp_query->query_vars['paged'] > 1 ? $pagenum = $wp_query->query_vars['paged'] : $pagenum = 1;
  162. /*2.2 从url中获取参数 即url中 0_0_0_0
  163. *将获取到的四个参数放入 $cons 数组中
  164. */
  165. global $wp_query;
  166. if( isset($wp_query->query_vars['condition']) && $wp_query->query_vars['condition']!='' ){
  167.   $condition = $wp_query->query_vars['condition'];
  168.   $conditions = explode('_',$condition);
  169.   $cons = array();
  170.   if(isset($conditions[0])){
  171.     $conditions[0] = (int)$conditions[0];
  172.   }else{
  173.     $conditions[0]=0;
  174.   }
  175.   if(isset($conditions[1])){
  176.     $conditions[1] = (int)$conditions[1];
  177.   }else{
  178.     $conditions[1]=0;
  179.   }
  180.   if(isset($conditions[2])){
  181.     $conditions[2] = (int)$conditions[2];
  182.   }else{
  183.     $conditions[2]=0;
  184.   }
  185.   if(isset($conditions[3])){
  186.     $conditions[3] = (int)$conditions[3];
  187.   }else{
  188.     $conditions[3]=0;
  189.   }
  190.   //从url中获取到的各分类法分类ID是否真实存在
  191.   if( in_array($conditions[0],$province_id) ){
  192.     $cons[0]=$conditions[0];
  193.   }else{
  194.     $cons[0]=0;
  195.   }
  196.   if( in_array($conditions[1],$city_id) ){
  197.     $cons[1]=$conditions[1];
  198.   }else{
  199.     $cons[1]=0;
  200.   }
  201.   if( in_array($conditions[2],$genre_id) ){
  202.     $cons[2]=$conditions[2];
  203.   }else{
  204.     $cons[2]=0;
  205.   }
  206.   if( in_array($conditions[3],$price_id) ){
  207.     $cons[3]=$conditions[3];
  208.   }else{
  209.     $cons[3]=0;
  210.   }
  211.   $sift_link = ashuwp_sift_link().'/'.$cons[0].'_'.$cons[1].'_'.$cons[2].'_'.$cons[3];
  212. }else{
  213.   $cons = array(0,0,0,0);
  214.   $sift_link = ashuwp_sift_link().'/0_0_0_0';
  215. }
  216. ?>
  217. <div class="sift_query">
  218. <div class="sift_cons">
  219. <div class="sift_li">
  220. <span>省:</span>
  221. <<?php if($cons[0]==0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/0_<?php echo $cons[1];?>_<?php echo $cons[2];?>_<?php echo $cons[3];?>/">不限</a>
  222. <?php
  223. foreach( $province_ob as $province ){
  224. ?>
  225.   <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $province->term_id; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[0] == $province->term_id){ echo 'class="current"'; } ?>><?php echo $province->name; ?></a>
  226. <?php } ?>
  227. </div>
  228. <div class="sift_li"><span>市:</span><a <?php if($cons[1] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_0_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>/">不限</a>
  229. <?php
  230. foreach( $city_ob as $city ){ ?>
  231.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $city->term_id; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[1] == $city->term_id){ echo 'class="current"'; } ?>><?php echo $city->name; ?></a>
  232. <?php } ?>
  233. </div>
  234. <div class="sift_li"><span>类型:</span><a <?php if($cons[2] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_0_<?php echo $cons[3];?>/">不限</a>
  235. <?php
  236. foreach( $genre_ob as $genre ){ ?>
  237.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $genre->term_id; ?>_<?php echo $cons[3];?>" <?php if($cons[2] == $genre->term_id){ echo 'class="current"'; } ?>><?php echo $genre->name; ?></a>
  238. <?php } ?>
  239. </div>
  240. <div class="sift_li"><span>价格:</span><a <?php if($cons[3] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_0/">不限</a>
  241. <?php
  242. foreach( $price_ob as $price ){ ?>
  243.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $price->term_id; ?>" <?php if($cons[3] == $price->term_id){ echo 'class="current"'; } ?>><?php echo $price->name; ?></a>
  244. <?php } ?>
  245. </div>
  246. </div>
  247. <?php
  248. //将获取到的参数组合为query_posts的参数
  249. $tax_query = array(
  250.     'relation'=> 'AND',
  251. );
  252. //province
  253. if( $cons[0] != 0 ){
  254.     $tax_query[] = array(
  255.         'taxonomy'=>'province',
  256.         'field'=>'id',
  257.         'terms'=>$cons[0]
  258.     );
  259. }
  260. //city
  261. if( $cons[1] != 0 ){
  262.     $tax_query[] = array(
  263.         'taxonomy'=>'city',
  264.         'field'=>'id',
  265.         'terms'=>$cons[1]
  266.     );
  267. }
  268. //genre
  269. if( $cons[2] != 0 ){
  270.     $tax_query[] = array(
  271.         'taxonomy'=>'genre',
  272.         'field'=>'id',
  273.         'terms'=>$cons[2]
  274.     );
  275. }
  276. //price
  277. if( $cons[3] != 0 ){
  278.     $tax_query[] = array(
  279.         'taxonomy'=>'price',
  280.         'field'=>'id',
  281.         'terms'=>$cons[3]
  282.     );
  283. }
  284. $args = array(
  285.     'paged' => $pagenum,
  286.     'tax_query'=> $tax_query
  287. );
  288. global $ashuwp_query;
  289. $ashuwp_query = new WP_Query( $args );
  290. ?>
  291. <div class="query_count">共找到<?php echo $ashuwp_query->found_posts;?>个符合条件的内容</div>
  292. </div>
  293. <?php
  294. if($ashuwp_query->have_posts()) : ?>
  295. <div id="post_list">
  296. <?php while($ashuwp_query->have_posts()) : $ashuwp_query->the_post(); ?>
  297. <div class="post">
  298.   <a href="<?php the_permalink();?>"><?php the_title();?></a>
  299. </div>
  300. <?php endwhile;?>
  301. </div>
  302. <?php endif; ?>
  303. <div id="ashuwp_page">
  304. <?php
  305. $pagination = paginate_links( array(
  306.   'base' => $links.'/page/%#%',
  307.   'format' => '/page/%#%',
  308.   'prev_text' => '上一页',
  309.   'next_text' => '下一页',
  310.   'total' => $ashuwp_query->max_num_pages,
  311.   'current' => $pagenum
  312. ) );
  313. if ( $pagination ) {
  314.     echo $pagination;
  315. }
  316. ?>
  317. </div>
  318. </div>
  319. </div><!--site-page-->
  320. <div id="footer">
  321. <div class="container">
  322. <p>阿树工作室,联系作者admin@treework.cn</p>
  323. </div>
  324. </div>
  325. </body>
  326. </html>

由此,添加完代码后,你可以尝试重新启用你的主题,看访问类似www.shouce.ren/sift地址的时候是否会出现404,如果是空白页,那就对了,因为我们的筛选页面模板page-sift.php还没有添加内容,所以是空白页。

步骤三、筛选页面模板

前面步骤二中,我们已经为筛选页面建立了页面模板page-sift.php,而且使用wordpress的url重写使得我们的筛选url能正确载入模板。接下来就要完善筛选页面的内容。

  1. //给post创建四个自定义分类法
  2. add_action('init', 'ashu_post_type');
  3. function ashu_post_type() {
  4.   register_taxonomy(
  5.     'province',
  6.     'post',
  7.     array(
  8.       'label' => '省',
  9.       'rewrite' => array( 'slug' => 'province' ),
  10.       'hierarchical' => true
  11.     )
  12.   );
  13.   register_taxonomy(
  14.     'city',
  15.     'post',
  16.     array(
  17.       'label' => '市',
  18.       'rewrite' => array( 'slug' => 'city' ),
  19.       'hierarchical' => true
  20.     )
  21.   );
  22.   register_taxonomy(
  23.     'genre',
  24.     'post',
  25.     array(
  26.       'label' => '类型',
  27.       'rewrite' => array( 'slug' => 'genre' ),
  28.       'hierarchical' => true
  29.     )
  30.   );
  31.   register_taxonomy(
  32.     'price',
  33.     'post',
  34.     array(
  35.       'label' => '价格',
  36.       'rewrite' => array( 'slug' => 'price' ),
  37.       'hierarchical' => true
  38.     )
  39.   );
  40. }
  41. //获取筛选页面的Url
  42. function ashuwp_sift_link(){
  43.   return home_url()."/sift";
  44. }
  45. /*
  46. *添加query变量
  47. */
  48. function ashuwp_query_vars($public_query_vars) {
  49.     $public_query_vars[] = 'ashuwp_page';
  50.     $public_query_vars[] = 'condition';
  51.     return $public_query_vars;
  52. }
  53. /*
  54. *sift页面的重写规则,三种url:
  55. *shouce.ren/sift   shouce.ren/sift/0_0_0_0/    shouce.ren/sift/0_0_0_0/page/2
  56. */
  57. function ashuwp_rewrite_rules( $wp_rewrite ){
  58.   $new_rules = array(
  59.     'sift/?$' => 'index.php?ashuwp_page=sift',
  60.     'sift/([^/]+)/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1),
  61.     'sift/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2)
  62.   );
  63.   $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  64. }
  65. /*
  66. *载入模板规则
  67. *用page-sift.php作为筛选页面的模板文件
  68. */
  69. function ashuwp_template_redirect(){
  70.   global $wp,$wp_query,$wp_rewrite;
  71.   if( !isset($wp_query->query_vars['ashuwp_page']) )
  72.     return;
  73.   $reditect_page =  $wp_query->query_vars['ashuwp_page'];
  74.   if ($reditect_page == "sift"){
  75.     include(get_template_directory().'/page-sift.php');
  76.     die();
  77.   }
  78. }
  79. /*
  80. *更新重写规则
  81. *激活主题的时候
  82. */
  83. function ashuwp_flush_rewrite_rules() {
  84.   global $pagenow, $wp_rewrite;
  85.   if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
  86.     $wp_rewrite->flush_rules();
  87. }
  88. add_action( 'load-themes.php', 'ashuwp_flush_rewrite_rules' );
  89. add_action('generate_rewrite_rules', 'ashuwp_rewrite_rules' );
  90. add_action('query_vars', 'ashuwp_query_vars');
  91. add_action("template_redirect", 'ashuwp_template_redirect');
  92. 筛选页面的内容较多,直接贴出,部分代码有注释。大概思路就是:
  93. 1. 先获取所有分类法的所有分类ID,有两个用途:判断从url获取的分类ID是否存在、输出筛选页面中筛选条件的url
  94. 2.url中获取四个ID参数。
  95. 3.输出筛选条件列表。注意判断当前访问、以及“不限”的条件
  96. 4.根据条件查询文章。
  97. 下面代码存在放本教程提供的主题的page-sift.php文件中
  98. <!DOCTYPE html>
  99. <html lang="zh-CN">
  100. <head>
  101. <meta charset="UTF-8">
  102. <title>阿树工作室--筛选页面教程</title>
  103. <link rel='stylesheet' id='ashuwp-style-css'  href='<?php echo get_stylesheet_uri(); ?>' type='text/css' media='all' />
  104. </head>
  105. <body>
  106. <div id="site-page">
  107. <div id="header">
  108. <h1 id="logo">
  109.  <a href="http://www.treework.cn">treework.cn</a>
  110. </h1>
  111. <h2 class="align-cenetr">阿树工作室--筛选页面教程</h2>
  112. </div>
  113. <div class="container content">
  114. <?php
  115. //1.1 获取所有province分类,将id放入 $province_id数组
  116. $args = array(
  117.   'taxonomy'=>'province',
  118.   'orderby'=>'id',
  119.   'hide_empty'=>0
  120. );
  121. $province_ob = get_categories( $args );
  122. $province_id = array();
  123. foreach($province_ob as $province){
  124.   $province_id[] = $province->term_id;
  125. }
  126. //1.2 获取所有city分类,将id放入 $city_id数组
  127. $args = array(
  128.   'taxonomy'=>'city',
  129.   'orderby'=>'id',
  130.   'hide_empty'=>0
  131. );
  132. $city_ob = get_categories( $args );
  133. $city_id = array();
  134. foreach($city_ob as $city){
  135.   $city_id[] = $city->term_id;
  136. }
  137. //1.3 获取所有genre分类,将id放入 $genre_id数组
  138. $args = array(
  139.   'taxonomy'=>'genre',
  140.   'orderby'=>'id',
  141.   'hide_empty'=>0
  142. );
  143. $genre_ob = get_categories( $args );
  144. $genre_id = array();
  145. foreach($genre_ob as $genre){
  146.   $genre_id[] = $genre->term_id;
  147. }
  148. //1.4 获取所有price分类,将id放入 $price_id数组
  149. $args = array(
  150.   'taxonomy'=>'price',
  151.   'orderby'=>'id',
  152.   'hide_empty'=>0
  153. );
  154. $price_ob = get_categories( $args );
  155. $price_id = array();
  156. foreach($price_ob as $price){
  157.   $price_id[] = $price->term_id;
  158. }
  159. //2 参数处理
  160. //2.1 页码
  161. $wp_query->query_vars['paged'] > 1 ? $pagenum = $wp_query->query_vars['paged'] : $pagenum = 1;
  162. /*2.2 从url中获取参数 即url中 0_0_0_0
  163. *将获取到的四个参数放入 $cons 数组中
  164. */
  165. global $wp_query;
  166. if( isset($wp_query->query_vars['condition']) && $wp_query->query_vars['condition']!='' ){
  167.   $condition = $wp_query->query_vars['condition'];
  168.   $conditions = explode('_',$condition);
  169.   $cons = array();
  170.   if(isset($conditions[0])){
  171.     $conditions[0] = (int)$conditions[0];
  172.   }else{
  173.     $conditions[0]=0;
  174.   }
  175.   if(isset($conditions[1])){
  176.     $conditions[1] = (int)$conditions[1];
  177.   }else{
  178.     $conditions[1]=0;
  179.   }
  180.   if(isset($conditions[2])){
  181.     $conditions[2] = (int)$conditions[2];
  182.   }else{
  183.     $conditions[2]=0;
  184.   }
  185.   if(isset($conditions[3])){
  186.     $conditions[3] = (int)$conditions[3];
  187.   }else{
  188.     $conditions[3]=0;
  189.   }
  190.   //从url中获取到的各分类法分类ID是否真实存在
  191.   if( in_array($conditions[0],$province_id) ){
  192.     $cons[0]=$conditions[0];
  193.   }else{
  194.     $cons[0]=0;
  195.   }
  196.   if( in_array($conditions[1],$city_id) ){
  197.     $cons[1]=$conditions[1];
  198.   }else{
  199.     $cons[1]=0;
  200.   }
  201.   if( in_array($conditions[2],$genre_id) ){
  202.     $cons[2]=$conditions[2];
  203.   }else{
  204.     $cons[2]=0;
  205.   }
  206.   if( in_array($conditions[3],$price_id) ){
  207.     $cons[3]=$conditions[3];
  208.   }else{
  209.     $cons[3]=0;
  210.   }
  211.   $sift_link = ashuwp_sift_link().'/'.$cons[0].'_'.$cons[1].'_'.$cons[2].'_'.$cons[3];
  212. }else{
  213.   $cons = array(0,0,0,0);
  214.   $sift_link = ashuwp_sift_link().'/0_0_0_0';
  215. }
  216. ?>
  217. <div class="sift_query">
  218. <div class="sift_cons">
  219. <div class="sift_li">
  220. <span>省:</span>
  221. <<?php if($cons[0]==0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/0_<?php echo $cons[1];?>_<?php echo $cons[2];?>_<?php echo $cons[3];?>/">不限</a>
  222. <?php
  223. foreach( $province_ob as $province ){
  224. ?>
  225.   <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $province->term_id; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[0] == $province->term_id){ echo 'class="current"'; } ?>><?php echo $province->name; ?></a>
  226. <?php } ?>
  227. </div>
  228. <div class="sift_li"><span>市:</span><a <?php if($cons[1] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_0_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>/">不限</a>
  229. <?php
  230. foreach( $city_ob as $city ){ ?>
  231.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $city->term_id; ?>_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>" <?php if($cons[1] == $city->term_id){ echo 'class="current"'; } ?>><?php echo $city->name; ?></a>
  232. <?php } ?>
  233. </div>
  234. <div class="sift_li"><span>类型:</span><a <?php if($cons[2] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_0_<?php echo $cons[3];?>/">不限</a>
  235. <?php
  236. foreach( $genre_ob as $genre ){ ?>
  237.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $genre->term_id; ?>_<?php echo $cons[3];?>" <?php if($cons[2] == $genre->term_id){ echo 'class="current"'; } ?>><?php echo $genre->name; ?></a>
  238. <?php } ?>
  239. </div>
  240. <div class="sift_li"><span>价格:</span><a <?php if($cons[3] == 0){ echo 'class="current"'; } ?> href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_0/">不限</a>
  241. <?php
  242. foreach( $price_ob as $price ){ ?>
  243.     <a href="<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0]; ?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_<?php echo $price->term_id; ?>" <?php if($cons[3] == $price->term_id){ echo 'class="current"'; } ?>><?php echo $price->name; ?></a>
  244. <?php } ?>
  245. </div>
  246. </div>
  247. <?php
  248. //将获取到的参数组合为query_posts的参数
  249. $tax_query = array(
  250.     'relation'=> 'AND',
  251. );
  252. //province
  253. if( $cons[0] != 0 ){
  254.     $tax_query[] = array(
  255.         'taxonomy'=>'province',
  256.         'field'=>'id',
  257.         'terms'=>$cons[0]
  258.     );
  259. }
  260. //city
  261. if( $cons[1] != 0 ){
  262.     $tax_query[] = array(
  263.         'taxonomy'=>'city',
  264.         'field'=>'id',
  265.         'terms'=>$cons[1]
  266.     );
  267. }
  268. //genre
  269. if( $cons[2] != 0 ){
  270.     $tax_query[] = array(
  271.         'taxonomy'=>'genre',
  272.         'field'=>'id',
  273.         'terms'=>$cons[2]
  274.     );
  275. }
  276. //price
  277. if( $cons[3] != 0 ){
  278.     $tax_query[] = array(
  279.         'taxonomy'=>'price',
  280.         'field'=>'id',
  281.         'terms'=>$cons[3]
  282.     );
  283. }
  284. $args = array(
  285.     'paged' => $pagenum,
  286.     'tax_query'=> $tax_query
  287. );
  288. global $ashuwp_query;
  289. $ashuwp_query = new WP_Query( $args );
  290. ?>
  291. <div class="query_count">共找到<?php echo $ashuwp_query->found_posts;?>个符合条件的内容</div>
  292. </div>
  293. <?php
  294. if($ashuwp_query->have_posts()) : ?>
  295. <div id="post_list">
  296. <?php while($ashuwp_query->have_posts()) : $ashuwp_query->the_post(); ?>
  297. <div class="post">
  298.   <a href="<?php the_permalink();?>"><?php the_title();?></a>
  299. </div>
  300. <?php endwhile;?>
  301. </div>
  302. <?php endif; ?>
  303. <div id="ashuwp_page">
  304. <?php
  305. $pagination = paginate_links( array(
  306.   'base' => $links.'/page/%#%',
  307.   'format' => '/page/%#%',
  308.   'prev_text' => '上一页',
  309.   'next_text' => '下一页',
  310.   'total' => $ashuwp_query->max_num_pages,
  311.   'current' => $pagenum
  312. ) );
  313. if ( $pagination ) {
  314.     echo $pagination;
  315. }
  316. ?>
  317. </div>
  318. </div>
  319. </div><!--site-page-->
  320. <div id="footer">
  321. <div class="container">
  322. <p>阿树工作室,联系作者admin@treework.cn</p>
  323. </div>
  324. </div>
  325. </body>
  326. </html>

文件下载

下载的文件为一个极简单的主题,运行一下步骤:

1.下载主题并安装,并设置好伪静态

2.添加几篇文章到自定义分类法中

3.直接访问筛选页面的url,比如www.shouce.ren/sift 或者www.shouce.ren/sift/0_1_1_0等等。

好了,阿树只能帮网友们到这了。

阿树工作室-筛选功能