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 empty函数 使用说明
Aug 10 PHP
php set_time_limit(0) 设置程序执行时间的函数
May 26 PHP
由php的call_user_func传reference引发的思考
Jul 23 PHP
关于file_get_contents返回为空或函数不可用的解决方案
Jun 24 PHP
php使用ereg验证文件上传的方法
Dec 16 PHP
Symfony2实现在controller中获取url的方法
Mar 18 PHP
PHP实现的统计数据功能详解
Dec 06 PHP
PHP实现一个多功能购物网站的案例
Sep 13 PHP
PHP基于关联数组20行代码搞定约瑟夫问题示例
Nov 07 PHP
PHP+Redis 消息队列 实现高并发下注册人数统计的实例
Jan 29 PHP
Yii框架的路由配置方法分析
Sep 09 PHP
PHP序列化和反序列化深度剖析实例讲解
Dec 29 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
PHP中使用php://input处理相同name值的表单数据
2015/02/03 PHP
php里array_work用法实例分析
2015/07/13 PHP
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
ThinkPHP5.0多个文件上传后找不到临时文件的修改方法
2018/07/30 PHP
Nginx+php配置文件及原理解析
2020/12/09 PHP
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
防止登录页面出现在frame中js代码
2014/07/22 Javascript
jquery判断至少有一个checkbox被选中的方法
2015/06/05 Javascript
JavaScript实现把数字转换成中文
2015/06/29 Javascript
jQuery实现调整表格单列顺序完整实例
2016/06/20 Javascript
Easyui Datagrid自定义按钮列(最后面的操作列)
2017/07/13 Javascript
详解在React-Native中持久化redux数据
2019/05/22 Javascript
关于JSON解析的实现过程解析
2019/10/08 Javascript
nodejs开发一个最简单的web服务器实例讲解
2020/01/02 NodeJs
vue:el-input输入时限制输入的类型操作
2020/08/05 Javascript
Vue实现简单的留言板
2020/10/23 Javascript
[02:14]2016国际邀请赛中国区预选赛Ehome晋级之路
2016/07/01 DOTA
Python Trie树实现字典排序
2014/03/28 Python
Django中模型Model添加JSON类型字段的方法
2015/06/17 Python
Python实现全角半角字符互转的方法
2016/11/28 Python
pandas和spark dataframe互相转换实例详解
2020/02/18 Python
python 如何调用远程接口
2020/09/11 Python
python 对象真假值的实例(哪些视为False)
2020/12/11 Python
详解tf.device()指定tensorflow运行的GPU或CPU设备实现
2021/02/20 Python
css3动画过渡实现鼠标跟随导航效果
2018/02/08 HTML / CSS
Furla官网:意大利著名的皮革品牌
2019/08/06 全球购物
PHP面试题大全
2015/10/16 面试题
Java程序开发中如何应用线程
2016/03/03 面试题
服务员自我评价
2014/01/25 职场文书
2014全国两会学习心得体会2000字
2014/03/10 职场文书
年终晚会主持词
2014/03/25 职场文书
三好学生演讲稿范文
2014/04/26 职场文书
保护环境建议书300字
2014/05/13 职场文书
参加招聘会后的感想
2015/08/10 职场文书
vite+vue3.0+ts+element-plus快速搭建项目的实现
2021/06/24 Vue.js