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参数实现路由功能的代码示例》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
wordpress截取首页摘要内容的办法

本文实例讲述了在wordpress截取首页摘要内容完成办法,分享给大家供大家参考。详细剖析如下: 这里截取就是利...

WordPress中限度非治理员用户在文章后只能评论一次

之前有网友提出,在WordPress中有没有方法完成每篇文章只容许用户评论一次? 暂不说这个需要有没有用,毕竟...

WordPress开发中自定义菜单的相干PHP函数应用简介

函数意义 wp_nav_menu () 自定义菜单显示函数 register_nav_menu () 自定义菜单注册函数 – 复数 register_...

封闭WordPress主动保留和文章修订性能

明天发文章时,惊奇的发现文章的ID居然曾经抵达了19了。鉴于我以前折腾Wordpress的经历,我晓得这是Wordpre...

Wordpress固定链接伪动态的设置办法

明天,想着在我机器上测试一下wordpress,尽管之前也有这个开源的顺序,可是不断没有装置呢,再加上最近不断...

如何在wordpress上添加文件上传的大小限度(多种完成办法)

本文只限Apache配置主机的应用办法,添加WordPress文件上传大小限度:经过配置php.ini的upload_max_filesiz...

需求提交

客服服务