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源代码中文乱码的处理办法

查看相干教程,UTF-8和UTF-8 + BOM有所区别,于是把function.php编码改为后者。成绩处理。 UFT-8 与UTF-8 B...

Wordpress完成自带的侧边栏和自定义的侧边栏同时存在的办法

本文实例讲述了Wordpress完成自带的侧边栏和自定义的侧边栏同时存在的办法。分享给大家供大家参考。详细如下...

WordPress导航菜单的滚动和淡入淡出成果的完成要点

滚动导航菜单 滚动菜单, 望文生义是以滚动的形式显示和暗藏菜单的. 其实跟淡入淡出菜单和滚动菜单的原理是一...

WordPress 避免歹意评论的办法

即便你的博客治理后盾的治理员身份被人破解了,普通他也不会同时破解你的网站空间ftp账号,所以他也更改不了...

wordpress制止复制内容的完成办法

1、右键复制时显示提醒语 把上面代码放到footer.php里即可 复制代码代码如下:<html> <head><title>防...

WordPress完成网站投稿者也能够上传图片的办法

本文实例讲述了WordPress完成网站投稿者也能够上传图片的办法。分享给大家供大家参考。详细剖析如下: Word...

需求提交

客服服务