wordpress自定义url参数实现路由功能的代码示例


Posted in PHP onNovember 28, 2013

经过两天的正则表达式的学习,和研究wordpress的路由函数,成功实现了自定义wordpress路由功能,以下是路由规则的实现。
如果有自定义的url参数,要通过路由传递,必须通过wordpress的函数将参数添加进去:

//add query_args
function add_query_vars($aVars) {
    $aVars[] = 'score';
    $aVars[] = 'type'; // represents the name of the product category as shown in the URL
    return $aVars;
}
add_filter('query_vars', 'add_query_vars');//wordpress过滤器

同时在获取参数的页面也要用到wordpress的函数获取:

$type=isset($wp_query->query_vars['type'])?urldecode($wp_query->query_vars['type']):'';
//路由规则-根据时间排序以及各类别的最新条目
function add_rewrite_rules($aRules) {
    $aNewRules = array(
        'text/([^latest][^/]+)/?(/page/([0-9]+)?)?/?$' => 'index.php?cat=2&score=$matches[1]&paged=$matches[3]',
        'image/([^latest][^/]+)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=3&score=$matches[1]&paged=$matches[3]',
        'video/([^latest][^/]+)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=4&score=$matches[1]&paged=$matches[3]',
        'resource/([^latest][^/]+)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=5&score=$matches[1]&paged=$matches[3]',
        'text/(latest)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=2&type=$matches[1]&paged=$matches[3]',
        'image/(latest)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=3&type=$matches[1]&paged=$matches[3]',
        'video/(latest)/?(/page/([0-9]+)?)?/?$'=>'index.php?cat=4&type=$matches[1]&paged=$matches[3]',
        'resource/(latest)/?$'=>'index.php?cat=5&type=$matches[1]',
        '(month)/?(/page/([0-9]+)?)?/?$'=>'index.php?score=$matches[1]&paged=$matches[3]',
        '(24hr)/?(/page/([0-9]+)?)?/?$'=>'index.php?score=$matches[1]&paged=$matches[3]',
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
//路由规则-类别
add_rewrite_rule('^text/?(/page/([0-9]+)?)?/?$','index.php?cat=2&paged=$matches[2]','top'); //对应的类别ID
add_rewrite_rule('^image/?(/page/([0-9]+)?)?/?$','index.php?cat=3&paged=$matches[2]','top');
add_rewrite_rule('^video/?(/page/([0-9]+)?)?/?$','index.php?cat=4&paged=$matches[2]','top'); 
add_rewrite_rule('^resource/?(/page/([0-9]+)?)?/?$','index.php?cat=5&paged=$matches[2]','top');
PHP 相关文章推荐
杏林同学录(六)
Oct 09 PHP
PHP 文件缓存的性能测试
Apr 25 PHP
php ob_flush,flush在ie中缓冲无效的解决方法
May 09 PHP
屏蔽机器人从你的网站搜取email地址的php代码
Nov 14 PHP
调整PHP的性能
Oct 30 PHP
php 表单提交大量数据发生丢失的解决方法
Mar 03 PHP
避免Smarty与CSS语法冲突的方法
Mar 02 PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 PHP
php使用curl打开https网站的方法
Jun 17 PHP
Yii2框架BootStrap样式的深入理解
Nov 07 PHP
基于laravel where的高级使用方法
Oct 10 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
Feb 15 PHP
PHP变量内存分配问题记录整理
Nov 27 #PHP
php遍历文件夹所有文件子文件夹函数代码
Nov 27 #PHP
PHP根据IP地址获取所在城市具体实现
Nov 27 #PHP
php编写的简单页面跳转功能实现代码
Nov 27 #PHP
关于JSON以及JSON在PHP中的应用技巧
Nov 27 #PHP
XAMPP安装与使用方法详细解析
Nov 27 #PHP
浅析echo(),print(),print_r(),return之间的区别
Nov 27 #PHP
You might like
JoshChen_php新手进阶高手不可或缺的规范介绍
2013/08/16 PHP
Thinkphp模板中截取字符串函数简介
2014/06/17 PHP
php实现文件下载代码分享
2014/08/19 PHP
php获取英文姓名首字母的方法
2015/07/13 PHP
Ext.FormPanel 提交和 Ext.Ajax.request 异步提交函数的区别
2009/11/12 Javascript
利用json获取字符出现次数的代码
2012/03/22 Javascript
精心挑选的15个jQuery下拉菜单制作教程
2012/06/15 Javascript
输入自动提示搜索提示功能的javascript:sugggestion.js
2013/09/02 Javascript
JS对select控件option选项的增删改查示例代码
2013/10/21 Javascript
js中replace的用法总结
2013/12/27 Javascript
javascript中的__defineGetter__和__defineSetter__介绍
2014/08/15 Javascript
深入解读JavaScript中的Hoisting机制
2015/08/12 Javascript
jQuery 移动端artEditor富文本编辑器
2016/01/11 Javascript
jQuery插件Flexslider实现图片轮播、图文结合滑动切换效果
2020/04/16 Javascript
JavaScript数值千分位格式化的两种简单实现方法
2016/08/01 Javascript
使用ngrok+express解决本地环境中微信接口调试问题
2018/02/26 Javascript
js中int和string数据类型互相转化实例
2019/01/16 Javascript
JS实现图片轮播效果实例详解【可自动和手动】
2019/04/04 Javascript
150行Node.js实现的dns代理工具
2019/08/02 Javascript
微信小程序 轮播图实现原理及优化详解
2019/09/29 Javascript
解析Python中的二进制位运算符
2015/05/13 Python
Scrapy爬虫实例讲解_校花网
2017/10/23 Python
Python subprocess模块功能与常见用法实例详解
2018/06/28 Python
3个用于数据科学的顶级Python库
2018/09/29 Python
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
2018/12/05 Python
Pytest参数化parametrize使用代码实例
2020/02/22 Python
Jupyter Notebook安装及使用方法解析
2020/11/12 Python
python 基于UDP协议套接字通信的实现
2021/01/22 Python
机电工程专业应届生求职信
2013/10/03 职场文书
市优秀教师事迹材料
2014/02/05 职场文书
公益活动邀请函
2014/02/05 职场文书
应用外语系自荐信
2014/06/26 职场文书
党的群众路线教育实践活动对照检查材料范文
2014/09/24 职场文书
物业保安辞职信
2015/05/12 职场文书
2016党员学习作风建设心得体会
2016/01/21 职场文书
python接口测试返回数据为字典取值方式
2022/02/12 Python