wordpress通过自定义分类法实现文章多条件筛选功能

WordPress教程 6219

前面博客吧已经分享过两篇关于wordpress多重条件搜索文章的教程,分别是《jQuery+自定义分类法实现wordpress多关键词筛选查询》、《通过wordpress自定义字段实现多重筛选文章搜索》,前两个教程可以较低程度地满足多条件查询要求,但对于需要不断增加查询条件的需要则略有不足。现在分享的这篇教程同样是出自阿树工作室,博客吧觉得灵活性更高。教程是根据房产信息网站希望访客能在网页上根据条件筛选符合条件的内容进行说明。

注意:该教程提供的方法仅能在Linux主机的伪静态状态下可用,win主机不可用。

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

wordpress通过自定义分类法实现文章多条件筛选功能

具有步骤:

1、建立自定义分类法。

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

把下面的代码添加到主题的functions.php文件:(下列代码存放在本教程提供的主题中的 include/taxonomy.php文件中)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//给post创建四个自定义分类法
add_action('init', 'ashu_post_type');
function ashu_post_type() {
  register_taxonomy(
	'province',
	'post',
	array(
	  'label' => '省',
	  'rewrite' => array( 'slug' => 'province' ),
	  'hierarchical' => true
	)
  );
  register_taxonomy(
	'city',
	'post',
	array(
	  'label' => '市',
	  'rewrite' => array( 'slug' => 'city' ),
	  'hierarchical' => true
	)
  );
  register_taxonomy(
	'genre',
	'post',
	array(
	  'label' => '类型',
	  'rewrite' => array( 'slug' => 'genre' ),
	  'hierarchical' => true
	)
  );
  register_taxonomy(
	'price',
	'post',
	array(
	  'label' => '价格',
	  'rewrite' => array( 'slug' => 'price' ),
	  'hierarchical' => true
	)
  );
}

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

wordpress通过自定义分类法实现文章多条件筛选功能

2、添加筛选默认,重写规则。

先在主题文件夹下面建立一个page-sift.php文件(文件名无要求,与下面代码中对应即可),这个文件将作为筛选页面的模板文件。然后添加重写规则,使得用户访问类似www.boke8.net/sift、www.boke8.net/sift/0_0_0_0类型url时,不会出现404,而且能正确加载模板。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//获取筛选页面的Url
function ashuwp_sift_link(){
  return home_url()."/sift";
}
/*
*添加query变量
*/
function ashuwp_query_vars($public_query_vars) {
	$public_query_vars[] = 'ashuwp_page';
	$public_query_vars[] = 'condition';
	return $public_query_vars;
}
/*
*sift页面的重写规则,三种url:
*ashuwp.com/sift   ashuwp.com/sift/0_0_0_0/    ashuwp.com/sift/0_0_0_0/page/2
*/
function ashuwp_rewrite_rules( $wp_rewrite ){
  $new_rules = array(
	'sift/?$' => 'index.php?ashuwp_page=sift',
	'sift/([^/]+)/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1),
	'sift/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?ashuwp_page=sift&condition='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2)
  );
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
/*
*载入模板规则
*用page-sift.php作为筛选页面的模板文件
*/
function ashuwp_template_redirect(){
  global $wp,$wp_query,$wp_rewrite;
  if( !isset($wp_query->query_vars['ashuwp_page']) )
	return;
  $reditect_page =  $wp_query->query_vars['ashuwp_page'];
  if ($reditect_page == "sift"){
	include(get_template_directory().'/page-sift.php');
	die();
  }
}
/*
*更新重写规则
*激活主题的时候
*/
function ashuwp_flush_rewrite_rules() {
  global $pagenow, $wp_rewrite;
  if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
	$wp_rewrite->flush_rules();
}
add_action( 'load-themes.php', 'ashuwp_flush_rewrite_rules' );
add_action('generate_rewrite_rules', 'ashuwp_rewrite_rules' );
add_action('query_vars', 'ashuwp_query_vars');
add_action("template_redirect", 'ashuwp_template_redirect');

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

3、筛选页面模板

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

筛选页面的内容较多,直接贴出,部分代码有注释。大概思路就是:先获取所有分类法的所有分类ID,有两个用途:判断从url获取的分类ID是否存在、输出筛选页面中筛选条件的url;从url中获取四个ID参数;输出筛选条件列表。注意判断当前访问、以及“不限”的条件;根据条件查询文章。

在page-sift.php文件中添加下面的代码:(下面代码存在放本教程提供的主题的page-sift.php文件中)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>阿树工作室--筛选页面教程</title>
<link rel='stylesheet' id='ashuwp-style-css'  href='<?php echo get_stylesheet_uri(); ?>' type='text/css' media='all' />
</head>
<body>
<div id="site-page">
<div id="header">
<h1 id="logo">
 <a href="http://www.treework.cn">treework.cn</a>
</h1>
<h2 class="align-cenetr">阿树工作室--筛选页面教程</h2>
</div>
<div class="container content">
<?php
//1.1 获取所有province分类,将id放入 $province_id数组
$args = array(
  'taxonomy'=>'province',
  'orderby'=>'id',
  'hide_empty'=>0
);
$province_ob = get_categories( $args );
$province_id = array();
foreach($province_ob as $province){
  $province_id[] = $province->term_id;
}
//1.2 获取所有city分类,将id放入 $city_id数组
$args = array(
  'taxonomy'=>'city',
  'orderby'=>'id',
  'hide_empty'=>0
);
$city_ob = get_categories( $args );
$city_id = array();
foreach($city_ob as $city){
  $city_id[] = $city->term_id;
}
//1.3 获取所有genre分类,将id放入 $genre_id数组
$args = array(
  'taxonomy'=>'genre',
  'orderby'=>'id',
  'hide_empty'=>0
);
$genre_ob = get_categories( $args );
$genre_id = array();
foreach($genre_ob as $genre){
  $genre_id[] = $genre->term_id;
}
//1.4 获取所有price分类,将id放入 $price_id数组
$args = array(
  'taxonomy'=>'price',
  'orderby'=>'id',
  'hide_empty'=>0
);
$price_ob = get_categories( $args );
$price_id = array();
foreach($price_ob as $price){
  $price_id[] = $price->term_id;
}
//2 参数处理
//2.1 页码
$wp_query->query_vars['paged'] > 1 ? $pagenum = $wp_query->query_vars['paged'] : $pagenum = 1;
/*2.2 从url中获取参数 即url中 0_0_0_0
*将获取到的四个参数放入 $cons 数组中
*/
global $wp_query;
if( isset($wp_query->query_vars['condition']) && $wp_query->query_vars['condition']!='' ){
  $condition = $wp_query->query_vars['condition'];
  $conditions = explode('_',$condition);
  $cons = array();
  if(isset($conditions[0])){
	$conditions[0] = (int)$conditions[0];
  }else{
	$conditions[0]=0;
  }
  if(isset($conditions[1])){
	$conditions[1] = (int)$conditions[1];
  }else{
	$conditions[1]=0;
  }
  if(isset($conditions[2])){
	$conditions[2] = (int)$conditions[2];
  }else{
	$conditions[2]=0;
  }
  if(isset($conditions[3])){
	$conditions[3] = (int)$conditions[3];
  }else{
	$conditions[3]=0;
  }
  //从url中获取到的各分类法分类ID是否真实存在
  if( in_array($conditions[0],$province_id) ){
	$cons[0]=$conditions[0];
  }else{
	$cons[0]=0;
  }
  if( in_array($conditions[1],$city_id) ){
	$cons[1]=$conditions[1];
  }else{
	$cons[1]=0;
  }
  if( in_array($conditions[2],$genre_id) ){
	$cons[2]=$conditions[2];
  }else{
	$cons[2]=0;
  }
  if( in_array($conditions[3],$price_id) ){
	$cons[3]=$conditions[3];
  }else{
	$cons[3]=0;
  }
  $sift_link = ashuwp_sift_link().'/'.$cons[0].'_'.$cons[1].'_'.$cons[2].'_'.$cons[3];
}else{
  $cons = array(0,0,0,0);
  $sift_link = ashuwp_sift_link().'/0_0_0_0';
}
?>
<div class="sift_query">
<div class="sift_cons">
<div class="sift_li">
<span>省:</span>
<a <?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>
<?php
foreach( $province_ob as $province ){
?>
  <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>
<?php } ?>
</div>
<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>
<?php
foreach( $city_ob as $city ){ ?>
	<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>
<?php } ?>
</div>
<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>
<?php
foreach( $genre_ob as $genre ){ ?>
	<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>
<?php } ?>
</div>
<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>
<?php
foreach( $price_ob as $price ){ ?>
	<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>
<?php } ?>
</div>
</div>
<?php
//将获取到的参数组合为query_posts的参数
$tax_query = array(
	'relation'=> 'AND',
);
//province
if( $cons[0] != 0 ){
	$tax_query[] = array(
		'taxonomy'=>'province',
		'field'=>'id',
		'terms'=>$cons[0]
	);
}
//city
if( $cons[1] != 0 ){
	$tax_query[] = array(
		'taxonomy'=>'city',
		'field'=>'id',
		'terms'=>$cons[1]
	);
}
//genre
if( $cons[2] != 0 ){
	$tax_query[] = array(
		'taxonomy'=>'genre',
		'field'=>'id',
		'terms'=>$cons[2]
	);
}
//price
if( $cons[3] != 0 ){
	$tax_query[] = array(
		'taxonomy'=>'price',
		'field'=>'id',
		'terms'=>$cons[3]
	);
}
$args = array(
	'paged' => $pagenum,
	'tax_query'=> $tax_query
);
global $ashuwp_query;
$ashuwp_query = new WP_Query( $args );
?>
<div class="query_count">共找到<?php echo $ashuwp_query->found_posts;?>个符合条件的内容</div>
</div>
<?php
if($ashuwp_query->have_posts()) : ?>
<div id="post_list">
<?php while($ashuwp_query->have_posts()) : $ashuwp_query->the_post(); ?>
<div class="post">
  <a href="<?php the_permalink();?>"><?php the_title();?></a>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<div id="ashuwp_page">
<?php
$pagination = paginate_links( array(
  'base' => $links.'/page/%#%',
  'format' => '/page/%#%',
  'prev_text' => '上一页',
  'next_text' => '下一页',
  'total' => $ashuwp_query->max_num_pages,
  'current' => $pagenum
) );
if ( $pagination ) {
	echo $pagination;
}
?>
</div>
</div>
</div><!--site-page-->
<div id="footer">
<div class="container">
<p>阿树工作室,联系作者admin@treework.cn</p>
</div>
</div>
</body>
</html>

教程代码文件下载:ashu-sift.zip

下载的文件为一个极简单的主题,效果预览步骤:

  1. 下载主题并安装,并设置好伪静态
  2. 添加几篇文章到自定义分类法中
  3. 直接访问筛选页面的url,比如www.boke8.net/sift 或者www.boke8.net/sift/0_1_1_0等等。

扩展:如何修改页面的标题

这里给出一个简单粗暴的范例,如果你需要更自由的修改方式,请参考官网关于wp_title函数的介绍:

1
2
3
4
5
6
7
8
9
10
//修改筛选页面的标题
add_filter('wp_title','ashuwp_sift_page_title',10,1);
function ashuwp_sift_page_title($title){
	global $wp_query;
	//与载入模板的判断类似
	if( isset($wp_query->query_vars['ashuwp_page']) && $wp_query->query_vars['ashuwp_page']=='sift' ){
	$title = '筛选页面-阿树工作室';
  }
	return $title;
}

教程来自:http://www.ashuwp.com/courses/highgrade/642.html

精品推荐: