Wordpress如何將列表URL結(jié)尾添加”/” 如何自定義類型的URL的固定鏈接
我們來分享兩篇關(guān)于Wordpress的URL優(yōu)化技巧的教程,將分類、標(biāo)簽(Tag)等列表URL結(jié)尾添加”/”斜線呢,添加自定義類型的URL的固定鏈接。
Wordpress如何將分類、標(biāo)簽(Tag)等列表URL結(jié)尾添加”/”斜線呢?
WordPress強(qiáng)大之處就是在于,我們能想到的已經(jīng)實(shí)現(xiàn)了,只是我們不知道而已。
網(wǎng)上有很多解決方案,基本歸類為:
1. 通過Rewrite Rule 301跳轉(zhuǎn)實(shí)現(xiàn)(如果是已經(jīng)被收錄的URL可以的,但剛開始時(shí)不建議使用)
2. 通過修改wp-includes/canonical.php實(shí)現(xiàn)(升級(jí)時(shí)會(huì)被覆蓋,不建議)
3. 通過插件來實(shí)現(xiàn)
下面就說一下插件方法:
插件 Permalink Trailing Slash Fixer 已經(jīng)實(shí)現(xiàn)這個(gè)功能。
下載后查看代碼,你會(huì)發(fā)現(xiàn)只有幾行而已,而且內(nèi)部調(diào)用的是Wordpress自帶的方法實(shí)現(xiàn)的,并且不會(huì)出現(xiàn)301跳轉(zhuǎn),個(gè)人認(rèn)為非常理想的解決方案.
代碼如下:
- /**
- * Public staff only.
- */
- if (is_admin()) return;
- $permalink_structure = get_option('permalink_structure');
- if (!$permalink_structure || '/' === substr($permalink_structure, -1))
- return;
- add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
- /**
- * Appends a trailing slash if it's missing in the permalink structure.
- *
- * Conditionally adds a trailing slash if the url type is not "single".
- *
- * @param string $url A URL with or without a trailing slash.
- * @param string $type The type of URL being considered (e.g. single, category, etc).
- * @return string The URL with the trailing slash fixed.
- */
- function ppm_fixe_trailingslash($url, $type)
- {
- if ('single' === $type)
- return $url;
- return trailingslashit($url);
- }
- /**
- * Public staff only.
- */
- if (is_admin()) return;
- $permalink_structure = get_option('permalink_structure');
- if (!$permalink_structure || '/' === substr($permalink_structure, -1))
- return;
- add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
- /**
- * Appends a trailing slash if it's missing in the permalink structure.
- *
- * Conditionally adds a trailing slash if the url type is not "single".
- *
- * @param string $url A URL with or without a trailing slash.
- * @param string $type The type of URL being considered (e.g. single, category, etc).
- * @return string The URL with the trailing slash fixed.
- */
- function ppm_fixe_trailingslash($url, $type)
- { //Vevb.com
- if ('single' === $type)
- return $url;
- return trailingslashit($url);
- }
是不是很簡潔…..
如果你是使用的多站點(diǎn),而之前的站點(diǎn)SEO還不錯(cuò),現(xiàn)在準(zhǔn)備添加一新站點(diǎn)的話,以防相互有影響建議將上面代碼微調(diào)后加入到 模板目錄functoins.php中,或是使用插件,只在新的站點(diǎn)開戶插件功能,也是一樣的:
- if (!is_admin()) {
- function ppm_fixe_trailingslash($url, $type)
- {
- $permalink_structure = get_option('permalink_structure');
- if (!$permalink_structure || '/' === substr($permalink_structure, -1))
- {
- return;
- }
- if ('single' === $type || 'page' === $type)
- {
- return $url;
- }
- return trailingslashit($url);
- }
- add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
- }
- if (!is_admin()) {
- function ppm_fixe_trailingslash($url, $type)
- {
- $permalink_structure = get_option('permalink_structure');
- if (!$permalink_structure || '/' === substr($permalink_structure, -1))
- {
- return;
- }
- if ('single' === $type || 'page' === $type)
- {
- return $url;
- }
- return trailingslashit($url);
- }
- add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
- }
最后,最得要的是,讓新添回報(bào)URL Rules生效:進(jìn)入后臺(tái)?>Setting?>Permalinks刷新即可.
Wordpress如何添加自定義類型的URL的固定鏈接
由于業(yè)務(wù)需要,偶爾會(huì)添加一種頁面類型,而這種類型不存在于Wordpress已有的routers 固定鏈接中,那么就需要我們來添加一種自定義的URL固定鏈接.
比如:
我需要添加一種/health/A~Z/的URL規(guī)則,那么如何自定義呢?
大體實(shí)現(xiàn)如下:
1.固定鏈接URL定義為:/health/A/
2.實(shí)際URL為:index.php?category_name=&idx=
實(shí)際代碼如下:
# 找到模板目錄下面的functions.php 添加下面代碼:
- function add_query_vars($aVars) {
- $aVars[] = "idx";
- return $aVars;
- }
- // hook add_query_vars function into query_vars
- add_filter('query_vars', 'add_query_vars');
- function add_rewrite_rules($aRules) {
- $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]');
- $aRules = $aNewRules + $aRules;
- return $aRules;
- }
- // hook add_rewrite_rules function into rewrite_rules_array
- add_filter('rewrite_rules_array', 'add_rewrite_rules');
- # 找到模板目錄下面的functions.php 添加下面代碼
- function add_query_vars($aVars) {
- $aVars[] = "idx";
- return $aVars;
- }
- // hook add_query_vars function into query_vars
- add_filter('query_vars', 'add_query_vars');
- function add_rewrite_rules($aRules) {
- $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]');
- $aRules = $aNewRules + $aRules;
- return $aRules;
- }
- // hook add_rewrite_rules function into rewrite_rules_array
- add_filter('rewrite_rules_array', 'add_rewrite_rules');
到這里已經(jīng)實(shí)現(xiàn)自定義URL規(guī)則的固定鏈接。
那么如何獲取URL中的參數(shù)呢?其實(shí)知道這是Wordpress自身的路由實(shí)現(xiàn)就好明白了,既然是WP的路由,那么我們就像別的框架一樣使用WP的獲取URL的參數(shù)的訪問就可以了.
在需要獲取idx參數(shù)值的寂寞中,如下代碼就可以得到了:
- #打印所有參數(shù)
- var_dump($wp_query->query_vars);
- #取得idx參數(shù)和值
- $idx = get_query_var('idx');
- #打印所有參數(shù)
- var_dump($wp_query->query_vars);
- #取得idx參數(shù)和值
- $idx = get_query_var('idx');
|
新聞熱點(diǎn)
疑難解答
圖片精選