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 实现多服务器共享 SESSION 数据
Aug 15 PHP
php zip文件解压类代码
Dec 02 PHP
用来解析.htgroup文件的PHP类
Sep 05 PHP
YII模块实现绑定二级域名的方法
Jul 09 PHP
php使用fopen创建utf8编码文件的方法
Oct 31 PHP
php中使用base HTTP验证的方法
Apr 20 PHP
php项目中百度 UEditor 简单安装调试和调用
Jul 15 PHP
php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
Dec 07 PHP
详解WordPress中调用评论模板和循环输出评论的PHP函数
Jan 05 PHP
PHP单例模式是什么 php实现单例模式的方法
May 14 PHP
PHP入门教程之面向对象基本概念实例分析
Sep 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
深入mysql_fetch_row()与mysql_fetch_array()的区别详解
2013/06/05 PHP
php结合js实现点击超链接执行删除确认操作
2014/10/31 PHP
input+select(multiple) 实现下拉框输入值
2009/05/21 Javascript
如何使用jQuery来处理图片坏链具体实现步骤
2013/05/02 Javascript
confirm的用法示例用于按钮操作时确定是否执行
2014/06/19 Javascript
JavaScript动态创建div等元素实例讲解
2016/01/06 Javascript
Vue.js第四天学习笔记(组件)
2016/12/02 Javascript
jQuery插件版本冲突的处理方法分析
2017/01/16 Javascript
jquery插件开发之选项卡制作详解
2017/08/30 jQuery
基于JavaScript实现幸运抽奖页面
2020/07/05 Javascript
vue 监听屏幕高度的实例
2018/09/05 Javascript
优雅的使用javascript递归画一棵结构树示例代码
2019/09/22 Javascript
微信小程序点击item使之滚动到屏幕中间位置
2020/03/25 Javascript
python遍历文件夹并删除特定格式文件的示例
2014/03/05 Python
几个提升Python运行效率的方法之间的对比
2015/04/03 Python
python执行等待程序直到第二天零点的方法
2015/04/23 Python
基于随机梯度下降的矩阵分解推荐算法(python)
2018/08/31 Python
Python爬虫学习之翻译小程序
2019/07/30 Python
python如何调用java类
2020/07/05 Python
Python在后台自动解压各种压缩文件的实现方法
2020/11/10 Python
css3实现六边形边框的实例代码
2019/05/24 HTML / CSS
html5的canvas元素使用方法介绍(画矩形、画折线、圆形)
2014/04/14 HTML / CSS
草莓网化妆品加拿大网站:Strawberrynet Canada
2016/09/20 全球购物
TheFork葡萄牙:欧洲领先的在线餐厅预订平台
2019/05/27 全球购物
EJB与JAVA BEAN的区别
2016/08/29 面试题
超市实习总结自我鉴定
2013/09/19 职场文书
实习生自我鉴定范文
2013/12/05 职场文书
消防战士优秀事迹材料
2014/02/13 职场文书
三八妇女节活动总结
2014/05/04 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
机械制造专业大学生自我鉴定
2014/09/19 职场文书
英语邀请函范文
2015/02/02 职场文书
统招统分证明
2015/06/23 职场文书
解析redis hash应用场景和常用命令
2021/08/04 Redis
vue3.0 数字翻牌组件的使用方法详解
2022/04/20 Vue.js
使用MybatisPlus打印sql语句
2022/04/22 SQL Server