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如何编写易读的代码
Jul 10 PHP
php 全局变量范围分析
Aug 07 PHP
php 常用类汇总 推荐收藏
May 13 PHP
解析thinkphp基本配置 convention.php
Jun 18 PHP
关于URL最大长度限制的相关资料查证
Dec 23 PHP
php中mysql连接方式PDO使用详解
Feb 25 PHP
PHP实现通用alert函数的方法
Mar 11 PHP
Centos下升级php5.2到php5.4全记录(编译安装)
Apr 03 PHP
php上传功能集后缀名判断和随机命名(强力推荐)
Sep 10 PHP
Zend Framework教程之配置文件application.ini解析
Mar 10 PHP
PHP中Cookie的使用详解(简单易懂)
Apr 28 PHP
laravel中的一些简单实用功能
Nov 03 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使用curl判断网页404(不存在)的方法
2016/06/23 PHP
php 常用的系统函数
2017/02/07 PHP
thinkphp5.1 文件引入路径问题及注意事项
2018/06/13 PHP
laravel 框架配置404等异常页面
2019/01/07 PHP
php写入文件不覆盖的实例讲解
2019/09/17 PHP
javascript中的void运算符语法及使用介绍
2013/03/10 Javascript
js 程序执行与顺序实现详解
2013/05/13 Javascript
给ListBox添加双击事件示例代码
2013/12/02 Javascript
jQuery实现的一个tab切换效果内部还嵌有切换
2014/08/10 Javascript
WEB前端设计师常用工具集锦
2014/12/09 Javascript
javascript多物体运动实现方法分析
2016/01/08 Javascript
jquery 获取select数组与name数组长度的实现代码
2016/06/20 Javascript
JavaScript正则表达式实例详解
2016/10/16 Javascript
vue.js 上传图片实例代码
2017/06/22 Javascript
JS获取浏览器地址栏的多个参数值的任意值实例代码
2018/07/24 Javascript
微信小程序使用map组件实现解析经纬度功能示例
2019/01/22 Javascript
mongodb初始化并使用node.js实现mongodb操作封装方法
2019/04/02 Javascript
微信小程序和百度的语音识别接口详解
2019/05/06 Javascript
Python爬虫天气预报实例详解(小白入门)
2018/01/24 Python
python调试神器PySnooper的使用
2019/07/03 Python
Flask教程之重定向与错误处理实例分析
2019/08/01 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
2019/08/29 Python
python 项目目录结构设置
2020/02/14 Python
python让函数不返回结果的方法
2020/06/22 Python
python中用ctypes模拟点击的实例讲解
2020/11/26 Python
css3.0 图形构成实例练习一
2013/03/19 HTML / CSS
英国最大的滑板品牌选择:Route One
2019/09/22 全球购物
哥德堡通行证:Gothenburg Pass
2019/12/09 全球购物
门诊挂号室室长岗位职责
2013/11/27 职场文书
环保倡议书500字
2014/05/15 职场文书
世界水日宣传活动总结
2015/02/09 职场文书
《叶问2》观后感
2015/06/15 职场文书
golang 实现时间戳和时间的转化
2021/05/07 Golang
解决Python中的modf()函数取小数部分不准确问题
2021/05/28 Python
Java常用函数式接口总结
2021/06/29 Java/Android
【海涛DOTA】D-cup邀请赛NV.cn vs DT.Love
2022/04/01 DOTA