wordpress进阶教程(四十):文章筛选功能
前几天一网友找我写一个筛选功能,他要做一个关于房产信息的网站,希望访客能在网页上根据条件筛选符合条件的文章。
根据他的功能要求,我给他的建议是,给每个筛选条件建立自定义分类法。比如他需求是根据四个条件来筛选,省、市、类型、价格。
其次,筛选页面我还是根据习惯使用了以前的伪静态方法,使访问的url地址类型为:www.ashuwp.com/sift/、www.ashuwp.com/sift/0_0_0_0、www.ashuwp.com/sift/1_23_33_32/ 后面以下划线分割的字符,就是我们要筛选的参数,分别为四个分类的ID。然后根据这四个ID值,来筛选符合条件的文章。最终效果:
要做这种功能,内容较多,文章最后面直接提供一个我已经写好了此功能的极简单主题,安装即可用。
注意:本文内容仅仅在伪静态状态下可用,win主机,不管你伪不伪静态,,,都勿用。
步骤一:建立自定义分类法。
本教程讲述的内容中用到了四个筛选条件:省、市、类型、价格。所以,我就给建立四个自定义分类法(使用默认的post)。使用下述代码,你可以直接复制粘贴到functions.php文件中。关于自定义分类法,你也可以访问wordpress进阶教程(三):创建自定义分类法
下列代码存放在本教程提供的主题中的 include/taxonomy.php文件中
- //给post创建四个自定义分类法
- add_action(‘init’, ‘ashu_post_type’);
- ashu_post_type() {
- register_taxonomy(
- ‘province’,
- ‘post’,
- (
- ‘label’ => ‘省’,
- ‘rewrite’ => ( ‘slug’ => ‘province’ ),
- ‘hierarchical’ => true
- )
- );
- register_taxonomy(
- ‘city’,
- ‘post’,
- (
- ‘label’ => ‘市’,
- ‘rewrite’ => ( ‘slug’ => ‘city’ ),
- ‘hierarchical’ => true
- )
- );
- register_taxonomy(
- ‘genre’,
- ‘post’,
- (
- ‘label’ => ‘类型’,
- ‘rewrite’ => ( ‘slug’ => ‘genre’ ),
- ‘hierarchical’ => true
- )
- );
- register_taxonomy(
- ‘price’,
- ‘post’,
- (
- ‘label’ => ‘价格’,
- ‘rewrite’ => ( ‘slug’ => ‘price’ ),
- ‘hierarchical’ => true
- )
- );
- }
添加完代码,应该在后台文章下面会多出四个自定义分类法:
步骤二:添加筛选默认,重写规则。
1. 先在主题文件夹下面建立一个page-sift.php文件(文件名无要求,与下面代码中对应即可),这个文件将作为筛选页面的模板文件。
2.添加重写规则,使得用户访问类似www.ashuwp.com/sift、www.ashuwp.com/sift/0_0_0_0类型url时,不会出现404,而且能正确加载模板。
关于wordpress重写规则,你也可以访问wordpress进阶教程(16):添加一个重写规则,构建新页面初试
下列代码存放在本教程提供的主题中的 include/rewrite.php文件中:
- //获取筛选页面的Url
- ashuwp_sift_link(){
- home_url().“/sift”;
- }
- /*
- *添加query变量
- */
- ashuwp_query_vars($public_query_vars) {
- $public_query_vars[] = ‘ashuwp_page’;
- $public_query_vars[] = ‘condition’;
- $public_query_vars;
- }
- /*
- *sift页面的重写规则,三种url:
- *ashuwp.com/sift ashuwp.com/sift/0_0_0_0/ ashuwp.com/sift/0_0_0_0/page/2
- */
- ashuwp_rewrite_rules( $wp_rewrite ){
- $new_rules = (
- ‘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作为筛选页面的模板文件
- */
- ashuwp_template_redirect(){
- $wp,$wp_query,$wp_rewrite;
- ( !isset($wp_query->query_vars[‘ashuwp_page’]) )
- ;
- $reditect_page = $wp_query->query_vars[‘ashuwp_page’];
- ($reditect_page == “sift”){
- (get_template_directory().’/page-sift.php’);
- ();
- }
- }
- /*
- *更新重写规则
- *激活主题的时候
- */
- ashuwp_flush_rewrite_rules() {
- $pagenow, $wp_rewrite;
- ( ‘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.ashuwp.com/sift地址的时候是否会出现404,如果是空白页,那就对了,因为我们的筛选页面模板page-sift.php还没有添加内容,所以是空白页。
步骤三、筛选页面模板
前面步骤二中,我们已经为筛选页面建立了页面模板page-sift.php,而且使用wordpress的url重写使得我们的筛选url能正确载入模板。接下来就要完善筛选页面的内容。
- <!DOCTYPE html>
- <html lang=“zh-CN”>
- <head>
- <meta charset=“UTF-8”>
- <title>wp自学笔记–筛选页面教程</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 =“align-cenetr”>wp自学笔记–筛选页面教程</h2>
- </div>
- <div =“container content”>
- <?php
- //1.1 获取所有province分类,将id放入 $province_id数组
- $args = (
- ‘taxonomy’=>’province’,
- ‘orderby’=>’id’,
- ‘hide_empty’=>0
- );
- $province_ob = get_categories( $args );
- $province_id = ();
- ($province_ob $province){
- $province_id[] = $province->term_id;
- }
- //1.2 获取所有city分类,将id放入 $city_id数组
- $args = (
- ‘taxonomy’=>’city’,
- ‘orderby’=>’id’,
- ‘hide_empty’=>0
- );
- $city_ob = get_categories( $args );
- $city_id = ();
- ($city_ob $city){
- $city_id[] = $city->term_id;
- }
- //1.3 获取所有genre分类,将id放入 $genre_id数组
- $args = (
- ‘taxonomy’=>’genre’,
- ‘orderby’=>’id’,
- ‘hide_empty’=>0
- );
- $genre_ob = get_categories( $args );
- $genre_id = ();
- ($genre_ob $genre){
- $genre_id[] = $genre->term_id;
- }
- //1.4 获取所有price分类,将id放入 $price_id数组
- $args = (
- ‘taxonomy’=>’price’,
- ‘orderby’=>’id’,
- ‘hide_empty’=>0
- );
- $price_ob = get_categories( $args );
- $price_id = ();
- ($price_ob $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 数组中
- */
- $wp_query;
- ( isset($wp_query->query_vars[‘condition’]) && $wp_query->query_vars[‘condition’]!=” ){
- $condition = $wp_query->query_vars[‘condition’];
- $conditions = explode(‘_’,$condition);
- $cons = ();
- (isset($conditions[0])){
- $conditions[0] = (int)$conditions[0];
- }{
- $conditions[0]=0;
- }
- (isset($conditions[1])){
- $conditions[1] = (int)$conditions[1];
- }{
- $conditions[1]=0;
- }
- (isset($conditions[2])){
- $conditions[2] = (int)$conditions[2];
- }{
- $conditions[2]=0;
- }
- (isset($conditions[3])){
- $conditions[3] = (int)$conditions[3];
- }{
- $conditions[3]=0;
- }
- //从url中获取到的各分类法分类ID是否真实存在
- ( in_array($conditions[0],$province_id) ){
- $cons[0]=$conditions[0];
- }{
- $cons[0]=0;
- }
- ( in_array($conditions[1],$city_id) ){
- $cons[1]=$conditions[1];
- }{
- $cons[1]=0;
- }
- ( in_array($conditions[2],$genre_id) ){
- $cons[2]=$conditions[2];
- }{
- $cons[2]=0;
- }
- ( in_array($conditions[3],$price_id) ){
- $cons[3]=$conditions[3];
- }{
- $cons[3]=0;
- }
- $sift_link = ashuwp_sift_link().’/’.$cons[0].’_’.$cons[1].’_’.$cons[2].’_’.$cons[3];
- }{
- $cons = (0,0,0,0);
- $sift_link = ashuwp_sift_link().’/0_0_0_0′;
- }
- ?>
- <div =“sift_query”>
- <div =“sift_cons”>
- <div =“sift_li”>
- <span>省:</span>
- <a <?php ($cons[0]==0){ echo ‘=“current”‘; } ?> href=“<?php echo ashuwp_sift_link(); ?>/0_<?php echo $cons[1];?>_<?php echo $cons[2];?>_<?php echo $cons[3];?>/”>不限</a>
- <?php
- ( $province_ob $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 ($cons[0] == $province->term_id){ echo ‘=“current”‘; } ?>><?php echo $province->name; ?></a>
- <?php } ?>
- </div>
- <div =“sift_li”><span>市:</span><a <?php ($cons[1] == 0){ echo ‘=“current”‘; } ?> href=“<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_0_<?php echo $cons[2]; ?>_<?php echo $cons[3];?>/”>不限</a>
- <?php
- ( $city_ob $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 ($cons[1] == $city->term_id){ echo ‘=“current”‘; } ?>><?php echo $city->name; ?></a>
- <?php } ?>
- </div>
- <div =“sift_li”><span>类型:</span><a <?php ($cons[2] == 0){ echo ‘=“current”‘; } ?> href=“<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_0_<?php echo $cons[3];?>/”>不限</a>
- <?php
- ( $genre_ob $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 ($cons[2] == $genre->term_id){ echo ‘=“current”‘; } ?>><?php echo $genre->name; ?></a>
- <?php } ?>
- </div>
- <div =“sift_li”><span>价格:</span><a <?php ($cons[3] == 0){ echo ‘=“current”‘; } ?> href=“<?php echo ashuwp_sift_link(); ?>/<?php echo $cons[0];?>_<?php echo $cons[1]; ?>_<?php echo $cons[2]; ?>_0/”>不限</a>
- <?php
- ( $price_ob $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 ($cons[3] == $price->term_id){ echo ‘=“current”‘; } ?>><?php echo $price->name; ?></a>
- <?php } ?>
- </div>
- </div>
- <?php
- //将获取到的参数组合为query_posts的参数
- $tax_query = (
- ‘relation’=> ‘AND’,
- );
- //province
- ( $cons[0] != 0 ){
- $tax_query[] = (
- ‘taxonomy’=>’province’,
- ‘field’=>’id’,
- ‘terms’=>$cons[0]
- );
- }
- //city
- ( $cons[1] != 0 ){
- $tax_query[] = (
- ‘taxonomy’=>’city’,
- ‘field’=>’id’,
- ‘terms’=>$cons[1]
- );
- }
- //genre
- ( $cons[2] != 0 ){
- $tax_query[] = (
- ‘taxonomy’=>’genre’,
- ‘field’=>’id’,
- ‘terms’=>$cons[2]
- );
- }
- //price
- ( $cons[3] != 0 ){
- $tax_query[] = (
- ‘taxonomy’=>’price’,
- ‘field’=>’id’,
- ‘terms’=>$cons[3]
- );
- }
- $args = (
- ‘paged’ => $pagenum,
- ‘tax_query’=> $tax_query
- );
- $ashuwp_query;
- $ashuwp_query = WP_Query( $args );
- ?>
- <div =“query_count”>共找到<?php echo $ashuwp_query->found_posts;?>个符合条件的内容</div>
- </div>
- <?php
- ($ashuwp_query->have_posts()) : ?>
- <div id=“post_list”>
- <?php ($ashuwp_query->have_posts()) : $ashuwp_query->the_post(); ?>
- <div =“post”>
- <a href=“<?php the_permalink();?>”><?php the_title();?></a>
- </div>
- <?php ;?>
- </div>
- <?php ; ?>
- <div id=“ashuwp_page”>
- <?php
- $pagination = paginate_links( (
- ‘base’ => $links.’/page/%#%’,
- ‘format’ => ‘/page/%#%’,
- ‘prev_text’ => ‘上一页’,
- ‘next_text’ => ‘下一页’,
- ‘total’ => $ashuwp_query->max_num_pages,
- ‘current’ => $pagenum
- ) );
- ( $pagination ) {
- echo $pagination;
- }
- ?>
- </div>
- </div>
- </div><!–site-page–>
- <div id=“footer”>
- <div =“container”>
- <p>wp自学笔记,联系作者admin@treework.cn</p>
- </div>
- </div>
- </body>
- </html>
文件下载
下载的文件为一个极简单的主题,效果预览步骤:
1.下载主题并安装,并设置好伪静态
2.添加几篇文章到自定义分类法中
3.直接访问筛选页面的url,比如www.ashuwp.com/sift 或者www.ashuwp.com/sift/0_1_1_0等等。
好了,阿树只能帮网友们到这了。
wp自学笔记-筛选功能
以下内容为2016年3月增加:
有网友提到如何修改页面的标题,这里给出一个简单粗暴的范例,如果你需要更自由的修改方式,请参考官网关于wp_title函数的介绍:
- //修改筛选页面的标题
- add_filter(‘wp_title’,’ashuwp_sift_page_title’,10,1);
- ashuwp_sift_page_title($title){
- $wp_query;
- //与载入模板的判断类似
- ( isset($wp_query->query_vars[‘ashuwp_page’]) && $wp_query->query_vars[‘ashuwp_page’]==’sift’ ){
- $title = ‘筛选页面-wp自学笔记’;
- }
- $title;
- }