wordpress自定义url参数完成路由性能的代码示例

通过两天的正则表白式的学习,和钻研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');

以上就是安达网络工作室关于《wordpress自定义url参数实现路由功能的代码示例》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
以JavaScript来完成WordPress中的二级导航菜单的办法

导航菜单 导航菜单早已 "深化民意", 在博客上的使用日益重要且多样. 从本文开端, 我将展开几个对于 WordPre...

WordPress 嵌套回复 (优缺陷对照)

1. 它能够进步用户体验, 调动访客回复的踊跃性, 从而添加评论的数量, 能让博客变得像社区一样活泼. 2. 博客...

优化WordPress中文章与评论的工夫显示

很多博客都喜爱用 评论宣布于 “XXX 分钟 之前”、文章宣布于 “XXX 分钟 之前”来显示文章评论的工夫,改善...

WordPress完成回复文章评论后发送邮件告诉的性能

本文实例讲述了WordPress完成回复文章评论后发送邮件告诉的性能。分享给大家供大家参考,详细如下: 很多时...

WordPress的JavaScript 本地化完成

目前对大少数人来说是一个无用的性能, 甚至有人以为这是 WordPress 卑劣地偷偷地在页面上埋了一个扯淡的脚本...

手工增加WordPress文章分享按钮(无插件)完成步骤

给一篇好的文章增加一些分享按钮分享到一些知名网站,很利于网站的推行。给站点增加分享按钮的办法很多,你...

需求提交

客服服务