前言介紹
- 這款 WordPress 外掛「If Shortcode」是 2014-03-01 上架。
- 目前有 200 個安裝啟用數。
- 上一次更新是 2024-08-02,距離現在已有 274 天。
- 外掛最低要求 WordPress 3.0.1 以上版本才可以安裝。
- 外掛要求網站主機運作至少需要 PHP 版本 5.4 以上。
- 有 6 人給過評分。
- 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。
外掛協作開發者
geomagas |
外掛標籤
if | else | shortcode | conditional | conditional-tags |
內容簡介
此外掛提供了「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.
各版本下載點
- 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
- 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「If Shortcode」來進行安裝。
(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。
0.1.0 | 0.2.0 | 0.2.1 | 0.3.0 | trunk |
延伸相關外掛(你可能也想知道)
VK Dynamic If Block 》網站架設者可以使用 VK Dynamic If Block 這個 WordPress 自訂區塊,透過特定條件去顯示或隱藏內部區塊。可以設定許多條件,例如顯示首頁、顯示單篇文章、顯...。
WordPress Conditional Content 》4>使用方法, 你可以在 if 簡碼(shortcode) 中放入條件式的內容,並使用其屬性來定義條件。簡碼之間的內容只有在所有已定義的條件都符合時才會顯示。, 定義...。
Optional Content 》這個外掛讓你可以藉由簡單的短代碼來顯示或隱藏頁面或文章內容。你不需要 PHP 知識或啟用 PHP 外掛。只需要在所見即所得編輯器中使用一個按鈕,選擇在什麼情...。
IF AS Shortcode 》此外掛可讓您在任何文章類型、選單和小工具中添加條件限制內容,並使用簡碼代碼而不需要使用 PHP 或其他語言:), 文章/頁面/其他文章類型:, , 創建新內容,...。
Conditions for Texts (Dynamic Content) 》條件文本WordPress外掛讓部落客在文章和頁面中使用IF和IF-ELSE條件。利用這個令人驚嘆的外掛,應用特定的條件,例如日期或時間,可以發布具體的內容和文字。,...。