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-MySQL教程归纳总结
Jun 07 PHP
php strlen mb_strlen计算中英文混排字符串长度
Jul 10 PHP
php 的加密函数 md5,crypt,base64_encode 等使用介绍
Apr 09 PHP
destoon首页调用求购供应信息的地区名称的方法
Aug 21 PHP
浅谈php命令行用法
Feb 04 PHP
php统计数组元素个数的方法
Jul 02 PHP
Yii2 GridView实现列表页直接修改数据的方法
May 16 PHP
php基于PDO连接MSSQL示例DEMO
Jul 13 PHP
php实现微信支付之企业付款
May 30 PHP
php app支付宝回调(异步通知)详解
Jul 25 PHP
让Laravel API永远返回JSON格式响应的方法示例
Sep 05 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
Oct 11 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中几种常见的超时处理全面总结
2012/09/11 PHP
php输出1000以内质数(素数)示例
2014/02/16 PHP
Laravel中encrypt和decrypt的实现方法
2017/09/24 PHP
Yii2.0框架模型多表关联查询示例
2019/07/18 PHP
JavaScript XML实现两级级联下拉列表
2008/11/10 Javascript
JS实现self的resend
2010/07/22 Javascript
jQuery Study Notes学习笔记 (二)
2010/08/04 Javascript
js通过八个点 拖动改变div大小的实现方法
2014/03/05 Javascript
JavaScript中按位“异或”运算符使用介绍
2014/03/14 Javascript
详解Vuejs2.0之异步跨域请求
2017/04/20 Javascript
JavaScript原型继承_动力节点Java学院整理
2017/06/30 Javascript
详解在微信小程序的JS脚本中使用Promise来优化函数处理
2019/03/06 Javascript
JS实现音乐钢琴特效
2020/01/06 Javascript
JS实现斐波那契数列的五种方式(小结)
2020/09/09 Javascript
[51:34]Ti4主赛事胜者组 DK vs EG 2
2014/07/19 DOTA
[02:27]2018DOTA2亚洲邀请赛趣味视频之钓鱼大赛 谁是垂钓冠军?
2018/04/05 DOTA
Python爬取Coursera课程资源的详细过程
2014/11/04 Python
python实现的二叉树定义与遍历算法实例
2017/06/30 Python
python使用pandas实现数据分割实例代码
2018/01/25 Python
selenium+python截图不成功的解决方法
2019/01/30 Python
python3+selenium自动化测试框架详解
2019/03/17 Python
Django web框架使用url path name详解
2019/04/29 Python
Python Django框架url反向解析实现动态生成对应的url链接示例
2019/10/18 Python
python os.path.isfile 的使用误区详解
2019/11/29 Python
Python换行与不换行的输出实例
2020/02/19 Python
python开根号实例讲解
2020/08/30 Python
css3 clip实现圆环进度条的示例代码
2018/02/07 HTML / CSS
自动化专业个人求职信范文
2013/11/29 职场文书
宿舍打麻将检讨书
2014/01/24 职场文书
会议主持人开场白台词
2015/05/28 职场文书
家装电话营销开场白
2015/05/29 职场文书
公司年会主持词范文!
2019/05/07 职场文书
2019年特色火锅店的创业计划书模板
2019/08/28 职场文书
关于springboot 配置date字段返回时间戳的问题
2021/07/25 Java/Android
springboot + mongodb 通过经纬度坐标匹配平面区域的方法
2021/11/01 MongoDB
Python语言内置数据类型
2022/02/24 Python