內容簡介
4>使用方法
你可以在 if 簡碼(shortcode) 中放入條件式的內容,並使用其屬性來定義條件。簡碼之間的內容只有在所有已定義的條件都符合時才會顯示。
定義條件
條件是通過在簡碼的屬性中設置以下格式來定義的:
[if <type>=<condition>]
條件式的內容
[/if]
一個基本的例子:
[if qs="product-type:shoes"]
謝謝你購買鞋子
[/if]
在這個例子中的文字只有在當前 URL 具有 GET 參數“product-type”並且該值為“shoes”時才會顯示。
可用的條件類型有:
qs – 匹配定義的查詢字符串/GET 參數的鍵/值對。查詢字符串條件的格式如下:
[if qs="<qskey>:<qsvalue>"] … [/if]
qskey 是查詢字符串變量的名稱,qsvalue 是要測試的值。例如,條件“qs="product-type:2"”將匹配“?product-type=2”。
referrer – 檢查當前來源網址。如果 HTTP_REFERER 包含 <value>,條件將為真。匹配不需要精確,因此如果用戶從 Google 而來,並且 <value> 設為“google.com”,條件將為真。
role – 匹配當前用戶的角色。匹配需要精確,因此“admin”將不匹配“administrator”。如果您想匹配未登錄的用戶,請使用空值的 role。
例子
根據查詢字符串顯示內容:
[if qs=”utm_source:partner-site”]
只有當當前 URL 包含 GET 參數 'utm_source' 並且其值為 'partner-site' 時才顯示此內容
[/if]
根據引薦網址顯示內容:
[if referrer="www.google.com"]
只有在引薦 URL 包含 'www.google.com' 時才顯示此內容
[/if]
根據用戶角色顯示內容:
[if role="editor"]
只有當用戶以角色 'editor' 登錄時才顯示此內容
[/if]
設置多個條件和結合不同類型的條件:
[if referrer="www.example.com" qs="utm_source:partner-site"]
此內容會顯示給來自 example.com 的用戶,他們點擊了來自我們的 RSS 給養的鏈接。
[/if]
使用“exact”或“contain”進行匹配
默認情況下,查詢字符串條件是根據簡碼中定義的精確值進行匹配的。如果想要更緩和的“通配符”匹配,可以添加一個值為“contain”的 match 屬性:
[if qs="product-type:cashmere-" match="contain"]
不錯的選擇!羊絨是一種絕妙的面料。
[/if]
這將匹配 ?product-type=cashmere-sweater 和 ?product-type=cashmere-coat。
在多個值上進行匹配
你可以使用分號作為分隔符,讓條件匹配多個值。例如:
[if qs="product-type:shoes;coat"]
這段文字顯示給買了漂亮的鞋子或時尚外套的人看。
[/if]
外掛標籤
開發者團隊
原文外掛簡介
How to use
You can place conditional content between if shortcodes, and use its attributes to define conditions. The content between the shortcodes will only be displayed if all defined conditions are met.
Defining conditions
Conditions are defined by setting the attributes of the shortcode in the following format:
[if
Conditional content
[/if]
A basic example:
[if qs="product-type:shoes"]
Thank you for buying shoes
[/if]
The text in this example will only be displayed if the current URL has a GET paramater of “product-type” with the value “shoes”.
Available condition types are:
qs – Match on the key/value pair of the defined query string/GET parameter. Query string conditions are formatted like this:
[if qs="
qskey is the name of the query string variable and `qsvalue` the value to test for. For example, the condition `qs="product-type:2"` will match `?product-type=2`.
referrer – To check on the current referrer. The condition will be true if HTTP_REFERER contains
role – Matches the current user’s role. The match needs to be exact so “admin” will not match “administrator”. Use role with and empty value if you want to match users that are not logged in.
Examples
Display content based on query string:
[if qs=”utm_source:partner-site”]
This content is only displayed if the current URL contains a GET paramater ‘utm_source’ with value ‘partner-site’
[/if]
Display content based on referrer:
[if referrer="www.google.com"]
This content is only displayed if the referring URL contains 'www.google.com'
[/if]
Display content based on user role:
[if role="editor"]
This content is only displayed if the user is logged in with the role 'editor'
[/if]
Setting multiple conditions and combining condition types:
[if referrer="www.example.com" qs="utm_source:partner-site"]
This content is displayed to users coming from example.com who clicked on a link originating from our RSS feed.
[/if]
Matching with `exact` or `contain`
By default query string conditions are matched on the exact value as defined in the shortcode. For looser ‘wildcard’ matching you can add a match attribute with the value contain:
[if qs="product-type:cashmere-" match="contain"]
Good choice! Cashmere is a wonderful fabric.
[/if]
This will match both ?product-type=cashmere-sweater and ?product-type=cashmere-coat
Matching on multiple values
You can have a condition match on multiple values by using the semicolon as a seperator when defining allowed values. Example:
[if qs="product-type:shoes;coat"]
This text is displayed for people who bought either a nice pair of shoes or a great fashionable coat.
[/if]
Nesting `if` statements
You can nest statements but you have to use iteration. This has to do with the limitations of the built-in shortcode API.
[if qs="tonight:the-night"]
Tonight's the night.
[if2 qs="future:beautiful"]
We create our own destiny every day we live.
[/if2]
[if2 qs="future:platic"]
I see sheets of plastic in your future.
[/if2]
[/if]
You can nest up to if4 (4 levels).
