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 相关文章推荐
php 友好URL的实现(吐血推荐)
Oct 04 PHP
PHP字符编码问题之GB2312 VS UTF-8解决方法
Jun 23 PHP
CURL状态码列表(详细)
Jun 27 PHP
PHP的PSR规范中文版
Sep 28 PHP
php遍历文件夹下的所有文件和子文件夹示例
Mar 20 PHP
php获取本周开始日期和结束日期的方法
Mar 09 PHP
php的4种常见运行方式
Mar 20 PHP
php变量与数组相互转换的方法(extract与compact)
Dec 02 PHP
LAMP环境使用Composer安装Laravel的方法
Mar 25 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
Jul 21 PHP
PHP 中魔术常量的实例详解
Oct 26 PHP
360搜索引擎自动收录php改写方案
Apr 28 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
本地机apache配置基于域名的虚拟主机详解
2013/08/10 PHP
ajax返回值中有回车换行、空格的解决方法分享
2013/10/24 PHP
JavaScript中String和StringBuffer的速度之争
2010/04/01 Javascript
js限制文本框为整数和货币的函数代码
2010/10/13 Javascript
js判断一个元素是否为另一个元素的子元素的代码
2012/03/21 Javascript
Ext GridPanel加载完数据后进行操作示例代码
2014/06/17 Javascript
Javascript中的匿名函数与封装介绍
2015/03/15 Javascript
Javascript中的数组常用方法解析
2016/06/17 Javascript
Js 获取当前函数参数对象的实现代码
2016/06/20 Javascript
JS中利用localStorage防止页面动态添加数据刷新后数据丢失
2017/03/10 Javascript
EasyUI中的dataGrid的行内编辑
2017/06/22 Javascript
vue2手机APP项目添加开屏广告或者闪屏广告
2017/11/28 Javascript
jQuery中图片展示插件highslide.js的简单dom
2018/04/22 jQuery
Vue CLI 3搭建vue+vuex最全分析(推荐)
2018/09/27 Javascript
layui 富文本编辑器和textarea值的相互传递方法
2019/09/18 Javascript
[34:10]Secret vs VG 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.24
2019/09/10 DOTA
Python脚本实现自动发带图的微博
2016/04/27 Python
Python用threading实现多线程详解
2017/02/03 Python
Python利用Beautiful Soup模块搜索内容详解
2017/03/29 Python
Python导入模块时遇到的错误分析
2017/08/30 Python
python查看模块安装位置的方法
2018/10/16 Python
python dlib人脸识别代码实例
2019/04/04 Python
pyinstaller打包多个py文件和去除cmd黑框的方法
2019/06/21 Python
Python 支持向量机分类器的实现
2020/01/15 Python
python json load json 数据后出现乱序的解决方案
2020/02/27 Python
基于Django signals 信号作用及用法详解
2020/03/28 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
美国最好的保健品打折网店:Swanson
2017/08/04 全球购物
信息专业本科生个人的自我评价
2013/10/28 职场文书
责任胜于能力演讲稿
2014/05/20 职场文书
自动化专业大学生职业生涯规划范文:爱拚才会赢
2014/09/12 职场文书
2015年英语教师工作总结
2015/05/20 职场文书
小学生暑假安全公约
2015/07/14 职场文书
聊聊Python中关于a=[[]]*3的反思
2021/06/02 Python
spring cloud 配置中心客户端启动遇到的问题
2021/09/25 Java/Android