一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

DEDECMS|帝國CMS|Discuz|PHPCMS|Wordpress|ZBLOG|ECSHOP|蘋果CMS|極致CMS|CMS系統|

服務器之家 - 建站程序 - Wordpress - wordpress自定義url參數實現路由功能的代碼示例

wordpress自定義url參數實現路由功能的代碼示例

2020-05-31 14:11WordPress教程網 Wordpress

這篇文章主要介紹了wordpress中實現路由功能的代碼示例,大家可以參考用代碼實現自己的路由功能

經過兩天的正則表達式的學習,和研究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');

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久爽狠狠添AV激情五月 | 美女国内精品自产拍在线播放 | 91制片厂制作传媒免费版樱花 | 日韩精品欧美国产精品亚 | 欧美日韩精品乱国产 | t66y地址一地址二地址三 | 国自产拍在线天天更新91 | 国产麻豆在线观看网站 | 欧美伊香蕉久久综合类网站 | 含羞草国产亚洲精品岁国产精品 | 日韩成人一区ftp在线播放 | 国产乱叫456在线 | 免费观看一级特黄三大片视频 | tube69xxxxhd日本 | 成人伊在线影院 | 久久久久免费视频 | 秋葵污视频 | 欧美人妖草草xxoo | 涩色网| 韩国日本香港毛片免费 | 国内精品久久久久影院网站 | 久久综合给会久久狠狠狠 | 果冻传媒在线完整免费观 | 春色视频网站 | 天美蜜桃精东乌鸦传媒 | 99re在线视频观看 | 亚洲国产精品无码中文字幕 | www.四虎在线观看 | 夫妻性生活影院 | 糖心vlog网页版 | 亚洲国产在线2o20 | 日本xxxxxx片免费播放18 | 91噜噜噜在线观看 | 精品免费tv久久久久久久 | 免费369看片入口 | 欧美一级片在线免费观看 | 免费观看美女被cao视频 | 女人和男人搞基 | 婷婷天天 | 国产日韩欧美在线观看不卡 | 色综合久久六月婷婷中文字幕 |