WordPress功能函數(shù)add_rewrite_tag()
wordpress功能函數(shù)add_rewrite_tag(),添加一個(gè)新的重寫標(biāo)記(如%postname%)。
用法:
add_rewrite_tag( string $tag, string $regex, string $query = '' )
描述:
$query參數(shù)是可選的。如果它被省略了,你必須確保你在' init '鉤子上或之前調(diào)用它。這是因?yàn)?query默認(rèn)為$tag=,為了使其工作,必須添加一個(gè)新的query var。
參數(shù):
$tag
(string) (必需) Name of the new rewrite tag.
$regex
(string) (必需) Regular expression to substitute the tag for in rewrite rules.
$query
(string) (可選) String to append to the rewritten query. Must end in '='.
默認(rèn)值: ''
更多信息
這個(gè)函數(shù)可以讓W(xué)ordPress知道自定義的querystring變量。通常,它與add_rewrite_rule()結(jié)合使用,為具有自定義模板的頁面創(chuàng)建重寫規(guī)則。
如果您使用這個(gè)函數(shù)聲明一個(gè)已經(jīng)存在的重寫標(biāo)記,那么現(xiàn)有的標(biāo)記將被覆蓋。
這個(gè)函數(shù)必須在init或更早的時(shí)候調(diào)用。
它的功能
通過剝離標(biāo)簽名稱中的%符號來獲取一個(gè)查詢var名稱:
使用名稱、生成的QV名稱和正則表達(dá)式調(diào)用$wp_rewrite->add_rewrite_tag()。
將QV添加為一個(gè)查詢var(同樣,這可以通過過濾query_vars來實(shí)現(xiàn),但是向WP類中添加一個(gè)像上面那樣存儲(chǔ)“額外的”QV的函數(shù)可能會(huì)更好)
來源:
文件: wp-includes/rewrite.php
function add_rewrite_tag( $tag, $regex, $query = '' ) {
// Validate the tag's name.
if ( strlen( $tag ) < 3 || '%' !== $tag[0] || '%' !== $tag[ strlen( $tag ) - 1 ] ) {
return;
}
global $wp_rewrite, $wp;
if ( empty( $query ) ) {
$qv = trim( $tag, '%' );
$wp->add_query_var( $qv );
$query = $qv . '=';
}
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
}
更新日志:

用戶貢獻(xiàn)的筆記
(bcworkz - 5年前貢獻(xiàn))
在下面的例子中,想象一個(gè)站點(diǎn)有一個(gè)自定義的分類“位置”,所有的帖子都被分配一個(gè)位置術(shù)語,如“巴黎”或“馬德里”。我們添加了一個(gè)重寫標(biāo)記“%location%”來建立位置查詢var。我們還添加了一個(gè)重寫規(guī)則,以便正確處理諸如example.com/goto/madrid/budget-lodging/這樣的URL。
add_action('init', 'add_my_rewrites');
function add_my_rewrites() {
add_rewrite_tag('%location%', '([^&]+)', 'location=');
add_rewrite_rule('^goto/([^/]*)/([^/]*)/?','index.php?location=$matches[1]&name=$matches[2]','top');
}
即使重寫標(biāo)簽看起來就像永久鏈接結(jié)構(gòu)標(biāo)簽,如果你試圖在永久鏈接結(jié)構(gòu)中使用重寫標(biāo)簽,由WordPress生成的url將看起來像example.com/goto/%location%/budget-lodging/。正確的術(shù)語并不像您所期望的那樣替代重寫標(biāo)記。要使您的標(biāo)記表現(xiàn)得像一個(gè)結(jié)構(gòu)標(biāo)記,請使用“post_link”過濾器用適當(dāng)?shù)男g(shù)語替換標(biāo)記。
// Assign value to %location% rewrite tag
add_filter('post_link', 'my_filter_post_link', 10, 2 );
function my_filter_post_link( $permalink, $post ) {
// bail if %location% tag is not present in the url:
if ( false === strpos( $permalink, '%location%'))
return $permalink;
$terms = wp_get_post_terms( $post->ID, 'location');
// set location, if no location is found, provide a default value.
if ( 0 < count( $terms ))
$location = $terms[0]->slug;
else
$location = 'timbuktu';
$location = urlencode( $location );
$permalink = str_replace('%location%', $location , $permalink );
return $permalink;
}
任何時(shí)候您更改與重寫API相關(guān)的東西,不要忘記刷新重寫規(guī)則!這可以通過進(jìn)入永久鏈接設(shè)置并單擊Save Changes來實(shí)現(xiàn),無需代碼。您實(shí)際上不需要在設(shè)置屏幕上做任何更改。
(由立花明- 4個(gè)月前貢獻(xiàn))
(從法典)
例子
下面將注冊一個(gè)名為' film_title '的標(biāo)簽:
function custom_rewrite_tag() {
add_rewrite_tag('%film_title%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
?>
當(dāng)您使用自定義頁面模板進(jìn)行重寫時(shí),這一點(diǎn)尤其重要。
檢索重寫后的URL的值
定義了重寫標(biāo)記之后,您現(xiàn)在可以使用WordPress的$wp_query變量來檢索重寫后的querystring變量的值。為了從重寫中獲得上述標(biāo)記的值,你可以在你的頁面模板中使用以下方法:
$wp_query->query_vars['film_title']
注意,在重寫的URL上使用$_GET將不起作用,即使重寫包含querystring變量。您必須使用$wp_query。