內容簡介
此外掛提供了「if」縮短碼來有條件地渲染內容。其語法如下:
[if condition1 condition2=false condition3]{content}[/if]
條件作為屬性名稱傳遞。多個條件的結果取所有條件的輸出或。
換句話說,如果至少一個條件的輸出為所需的布爾結果,則呈現 {content},否則將其丟棄。
屬性值決定我們是否希望條件為真或假。值為「0」、「false」、「空字符串」、「null」或「no」表示我們期望該條件為假。
包括該值的缺席在內的任何其他值都解釋為真。
例如,假設 我們想將一個句子包含在文章中,但僅限匿名訪客:
[if is_user_logged_in=no]“該句子。”[/if]
插件還提供了一個 [else] 縮短碼和一個 [eitherway] 縮短碼,供在 [if] 區塊內使用。[else] 將呈現其內容(如果條件為假),[eitherway] 無論評估結果如何都將呈現其內容。
當在 [if] 區塊之外使用時,這些縮短碼的行為就像整個內容都被包裹在一個條件為真的 [if] 縮短碼中一樣。
插入多個這些縮短碼後即可使用。這使您能夠做出以下操作:
- 我是否已經登錄?
[if is_user_logged_in]- 是,你已經登錄了。
[else]- 不,你還沒有登錄。
[/else][eitherway]- 對不起,我聽不懂。
[/eitherway]- 我說了,你已經登錄了!
[else]- 你沒有登錄!你怎麼回事?[/else][/if]
默認支持眾多條件。以下條件的評估結果相當於使用無參數語法調用對應的 WordPress 條件標記:
is_single
is_singular
is_page
is_home
is_front_page
is_category
is_tag
is_tax
is_sticky
is_author
is_archive
is_year
is_month
is_day
is_time
is_feed
is_search
comments_open
pings_open
is_404
is_user_logged_in
is_super_admin
例如,is_page條件的評估相當於不使用參數調用 is_page()。
插件的功能可以通過其他插件進行擴展,通過過濾器添加自定義條件。
要添加自定義條件,必須以以下方式定義過濾器鉤子:
add_filter($if_shortcode_filter_prefix.'my_condition','my_condition_evaluator');
function my_condition_evaluator($value)
{
$evaluate=.... /* 在此添加您的評估代碼 */
return $evaluate;
}
非常感謝 M Miller 提供的 normalize_empty_atts() 函数,可在此处找到:http://wordpress.stackexchange.com/a/123073/39275。
外掛標籤
開發者團隊
原文外掛簡介
This plugin provides an [if] shortcode to conditionally render content. The syntax is the following:
[if condition1 condition2=false condition3]{content}[/if]
Conditions are passed as attribute names. Multiple conditions evaluate to the result of ORing all of them. In other words, if at least one condition evaluates to the desired boolean result, {content} is rendered, otherwise it is discarded.
Attribute values determine if we want the condition to be true or false. A value of '0', 'false', '' (the empty string), 'null' or 'no' means we expect the condition to be false. Anything else, including the absense of a value, is interpreted as true.
For example, suppose that we want to include a sentence in a post, but only for anonymous visitors:
[if is_user_logged_in=no]The Sentence.[/if]
It also provides an [else] shortcode and an [eitherway] one for use inside [if] blocks. [else] will render its content if the condition evaluates to false, and [eitherway] will render its content regardless of the evaluation result.
When used outside an [if] block, these shortcodes behave as if the whole content is surrounded by an [if] shortcode whose condition evaluates to true. In other words, an [else] shortcode would not render any content, while a [eitherway] one would. You can use as many of these shortcodes as you like in a single [if] block, which gives you the ability to do things like:
- Am I logged in?
[if is_user_logged_in]- Yes you are.
[else]- No you are not.
[/else][eitherway]- I'm sorry, what?
[/eitherway]- I said YOU A-R-E LOGGED IN!!!
[else]- YOU ARE NOT LOGGED IN!!! What's the matter with you?[/else][/if]
A multitude of conditions are supported out-of-the-box. The following evaluate to the result of the corresponding WordPress Conditional Tag, using the no-parameter syntax:
is_single
is_singular
is_page
is_home
is_front_page
is_privacy_policy
is_attachment
is_category
is_tag
is_tax
is_author
is_archive
is_year
is_month
is_date
is_day
is_time
is_feed
is_search
is_sticky
is_preview
has_term
has_excerpt
comments_open
pings_open
is_404
is_user_logged_in
is_super_admin
is_multi_author
is_multisite
is_main_site
is_child_theme
For example, the evaluation of the is_page condition is equivalent to calling is_page() with no parameter.
Extending
The functionality of the plugin can be extended by other plugins, by means of adding custom conditions through filters. To add a custom condition, a filter hook must be defined in the following manner:
add_filter($if_shortcode_filter_prefix.'my_condition','my_condition_evaluator');
function my_condition_evaluator($value) {
$evaluate=.... /* add your evaluation code here */
return $evaluate;
}
Then, you can use your custom condition like so:
[if my_condition]{content}[/if]
A big thanks to M Miller for the normalize_empty_atts() function.
