前言介紹
- 這款 WordPress 外掛「Widget Logic」是 2008-02-27 上架。
- 目前有 100000 個安裝啟用數。
- 上一次更新是 2025-03-21,距離現在已有 43 天。
- 外掛最低要求 WordPress 3.0 以上版本才可以安裝。
- 外掛要求網站主機運作至少需要 PHP 版本 5.4 以上。
- 有 185 人給過評分。
- 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。
外掛協作開發者
外掛標籤
blocks | widget | sidebar | conditional-tags | gutenberg widgets |
內容簡介
此外掛可在每個小工具中新增一個名為 "Widget logic" 的額外控制欄,讓您控制這個小工具會出現在哪些頁面上。文字欄位允許您使用 WP 的Conditional Tags或任何通用的 PHP 程式碼。
請注意,您所輸入的 Widget logic 會直接被 EVAL,任何有權限編輯小工具外觀的人都可以添加任何程式碼,包括惡意和可能具有破壞性的功能。此外,有一個可選的篩選器 'widget_logic_eval_override' 可供您使用,以繞過 EVAL,使用您自己的程式碼(請參閱其他注意事項)。
配置和選項在通常的小工具管理介面中。
配置
除了透過這個外掛程式對小工具進行條件邏輯外,還有三個選項被添加到小工具管理頁面的底部(請見屏幕截圖)。
使用 'wp_reset_query' 修復 — WP 的許多功能,以及許多主題和外掛程式,都可能干擾條件標記,使 is_home 不在首頁上是真的。通常可以透過在呼叫小工具之前快速使用 wp_reset_query() 陳述式來解決這個問題,這個選項可以替您加入這個程式碼,而不必編輯程式碼。
Load logic — 這個選項允許您設置頁面加載時點檢查小工具邏輯的時間。在 v.50 之前,它是在 'wp_head' 觸發時(也就是 HTML 的 HEAD 區塊建立期間)發生的。許多主題並沒有呼叫 wp_head,這是一個問題。從 v.50 開始,預設情況下,這個過程盡可能早,即當這個外掛程式加載時就開始。您現在可以指定這些「後期加載」點(按時間順序):
在主題載入後 (after_setup_theme 觸發)
在所有 PHP 載入後 (wp_loaded 觸發)
在查詢變數設置後 (parse_query 觸發) - 這是預設值
在頁面header區塊期間 (wp_head 觸發)
如果您的邏輯取決於定義在主題 functions.php 文件中的函數,則可能需要延遲載入。相反,如果您想確保小工具計數正確地計算,例如,在側邊欄沒有小工具時顯示替代版面或內容,則可能希望盡早載入它。
Do not cache widget logic results — 從 v.58 開始,widget logic 程式碼只需執行一次就可以,但這可能會導致某些主題出現意外結果,因此這個選項可用來關閉這個行為。(每次調用 sidebars_widgets 篩選器時都會評估程式碼的真/假)。
撰寫邏輯程式碼
"Widget logic" 欄位中的文字可以是完整的 PHP 程式碼,並應在您需要小工具出現時返回 "true"。如果沒有 "return" 在文字中,將在開頭隱式添加 "return",在結尾添加 ";"。(這只是使像 is_home() 這樣的單個語句更方便。)
基本原理
充分利用WP 的條件標記。您可以使用下列方式變化和結合代碼:
!(NOT)來反轉邏輯,例如 !is_home() 當不是首頁時是 TRUE。
||(OR)來組合條件。當 X 為真或 Y 為真時,X OR Y 為 TRUE。
&&(AND)使條件更加具體。X AND Y 在 X 和 Y 都為 TRUE 時為 TRUE。
WP論壇和網站上有許多出色的代碼示例。但 WP Codex 也是一個有用的資源,值得參考。
原文外掛簡介
This plugin gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.
PLEASE NOTE The widget logic you introduce is EVAL’d directly. Anyone who has access to edit widget appearance will have the right to add any code, including malicious and possibly destructive functions. There is an optional filter ‘widget_logic_eval_override’ which you can use to bypass the EVAL with your own code if needed. (See Other Notes).
The configuring and options are in the usual widget admin interface.
BIG UPDATE:
Now you can control widget in Gutenberg Widgets editor as well as in Classic Editor. It is just as easy as before but also in gutenberg view.
Pre-installed widgets let you add special widget with one click of the mouse. First pre-installed widget is Live Match that let you add widget of one random live football game with real time score updates (teams logos, livescore, minute of the match, tournament name). And more interesting widgets to come!
Configuration
Aside from logic against your widgets, there are three options added to the foot of the widget admin page (see screenshots).
Use ‘wp_reset_query’ fix — Many features of WP, as well as the many themes and plugins out there, can mess with the conditional tags, such that is_home is NOT true on the home page. This can often be fixed with a quick wp_reset_query() statement just before the widgets are called, and this option puts that in for you rather than having to resort to code editing
Load logic — This option allows you to set the point in the page load at which your widget logic if first checked. Pre v.50 it was when the ‘wp_head’ trigger happened, ie during the creation of the HTML’s HEAD block. Many themes didn’t call wp_head, which was a problem. From v.50 it happens, by default, as early as possible, which is as soon as the plugin loads. You can now specify these ‘late load’ points (in chronological order):
after the theme loads (after_setup_theme trigger)
when all PHP loaded (wp_loaded trigger)
after query variables set (parse_query) – this is the default
during page header (wp_head trigger)
You may need to delay the load if your logic depends on functions defined, eg in the theme functions.php file. Conversely you may want the load early so that the widget count is calculated correctly, eg to show an alternative layour or content when a sidebar has no widgets.
Don’t cache widget logic results — From v .58 the widget logic code should only execute once, but that might cause unexpected results with some themes, so this option is here to turn that behaviour off. (The truth/false of the code will be evaluated every time the sidebars_widgets filter is called.
Interaction with External Services
Widget Logic uses the external service to obtain up-to-date information about the results of football matches. widgetlogic.org is a source of sports information, that provides a wide range of information about football, including various leagues, tournaments, and championships from around the world.
The functioning of the widgetlogic.org service is based on delivering real-time data about selected matches without the need to refresh the page. This means that data is automatically updated without requiring page reload. This approach ensures users quick and uninterrupted access to the latest sports data without the effort of manually updating information, allowing them to stay informed about ongoing events in real-time.
Writing Logic Code
The text in the ‘Widget logic’ field can be full PHP code and should return ‘true’ when you need the widget to appear. If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end. (This is just to make single statements like is_home() more convenient.)
The Basics
Make good use of WP’s own conditional tags. You can vary and combine code using:
! (NOT) to reverse the logic, eg !is_home() is TRUE when this is NOT the home page.
|| (OR) to combine conditions. X OR Y is TRUE when either X is true or Y is true.
&& (AND) to make conditions more specific. X AND Y is TRUE when both X is true and Y is true.
There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as Test if post is in a descendent category.
Examples
is_home() — just the main blog page
!is_page('about') — everywhere EXCEPT this specific WP ‘page’
!is_user_logged_in() — shown when a user is not logged in
is_category(array(5,9,10,11)) — category page of one of the given category IDs
is_single() && in_category('baked-goods') — single post that’s in the category with this slug
current_user_can('level_10') — admin only widget
strpos($_SERVER['HTTP_REFERER'], "google.com")!=false — widget to show when clicked through from a google search
is_category() && in_array($cat, get_term_children( 5, 'category')) — category page that’s a descendent of category 5
global $post; return (in_array(77,get_post_ancestors($post))); — WP page that is a child of page 77
global $post; return (is_page('home') || ($post->post_parent=="13")); — home page OR the page that’s a child of page 13
Note the extra ‘;’ on the end where there is an explicit ‘return’.
The ‘widget_logic_eval_override’ filter
Before the Widget Logic code is evaluated for each widget, the text of the Widget Logic code is passed through this filter. If the filter returns a BOOLEAN result, this is used instead to determine if the widget is visible. Return TRUE for visible.
各版本下載點
- 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
- 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Widget Logic」來進行安裝。
(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。
0.2 | 0.3 | 0.4 | 0.31 | 0.42 | 0.43 | 0.44 | 0.45 | 0.46 | 0.47 | 0.48 | 0.50 | 0.51 | 0.52 | 0.54 | 0.55 | 0.56 | 0.57 | 6.02 | 5.7.0 | 5.7.1 | 5.7.2 | 5.7.3 | 5.7.4 | 5.8.0 | 5.8.1 | 5.8.2 | 5.9.0 | 6.0.0 | 6.0.1 | 6.0.3 | 6.0.4 | 6.0.5 | trunk | 5.10.2 | 5.10.3 | 5.10.4 |
延伸相關外掛(你可能也想知道)
Fixed Widget and Sticky Elements for WordPress 》使用 Fixed Widget 外掛可以創建黏貼式的小部件、區塊和其他元素,當用戶向上或向下滾動頁面時,它們會保持在可見的屏幕區域內。, 相較於非固定小工具,黏貼...。
Image Widget 》Image Widget 是一個簡單的外掛程式,利用 WordPress 原生媒體管理員,為您的網站新增圖片小工具。, 需要新增幻燈片、燈箱或隨機圖像嗎?, 請查看 Image Widg...。
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager 》使用 Custom Sidebars,一個靈活的小工具管理器,在您的網站上管理和替換側邊欄和其他小工具區。, 製作自訂側邊欄配置,能夠選擇在您網站的每個頁面或文章上...。
Widgets on Pages 》這是添加小工具到文章和/或頁面最簡單,也是評價最高的方法。您可以創建無限的動態側邊欄(小工具區域),把它們插入到 WordPress 文章或頁面中。, 從設置菜...。
Ocean Custom Sidebar 》這個 WordPress 外掛可以生成無限數量的側邊欄,您可以把它們放置在想要的任何頁面上。前往主題面板 > 側邊欄以創建自定義側邊欄。, 此外掛需要安裝 Ocean...。
Content Aware Sidebars – Fastest Widget Area Plugin 》CAS 是迄今最快、最強大的 WordPress 側邊欄外掛程式。您可以在幾秒鐘內建立量身定制的小工具區。, 通過在不同的條件下顯示不同的小工具區,您可以優化轉化率...。
Simple Image Widget 》Simple Image Widget就如其名般簡單易用,是在側邊欄中添加圖像的最簡單方法。您可以展示廣告、呼籲行動,甚至基於圖像小工具構建滑塊。, 儘管它很簡單,但Si...。
Meks Simple Flickr Widget 》這個 WordPress 外掛可以快速地將你的 Flickr 照片顯示於 Widget 中,且不需要進行授權(只需提供你的使用者 ID)。, 功能, , 無需授權(只需提供使用者 ID)...。
WP Page Post Widget Clone 》這個外掛只需要點擊一下,就能複製一篇文章或頁面。現在您無需從頭開始設計新的頁面或文章。, 這個外掛的主要功能包括:, , 創建頁面副本,保留所有內容和標...。
SMK Sidebar Generator 》這個外掛可以生成任意數量的側邊欄,然後讓你將它們放置在你想要的任何頁面。, , 特色:, , 無限數量的側邊欄。, 使用條件替換預設主題側邊欄,或全局替換只...。
Sidebar Login 》e links., sidebar_login_widget_end – Fired after the widget., , 這是一個 WordPress 的外掛,Sidebar Login 可以為您的 WordPress 博客添加一個有...。
Pojo Sidebars 》使用 Pojo Sidebars 您可以為任何頁面創建所需的多個側邊欄,特別是針對基於 Pojo Framework 的主題。, 您可以輕鬆地將您的網站分為區域,同時為每個單獨的...。
NewStatPress 》NewStatPress 是 StatPress 的新版本,是第一個能即時追蹤您的部落格訪問統計資訊的 WordPress 外掛。它能夠收集有關訪客、搜尋引擎爬蟲、搜尋關鍵字、訪客的...。
Newpost Catch 》在新文章設置小工具中的縮略圖設置。。
WP Sticky Sidebar – Floating Sidebar On Scroll for Any Theme 》WP Sticky Sidebar 外掛可以讓您的浮動側邊欄一直顯示,當使用者滾動網頁時,它將永久可見。預設側邊欄的最大問題是只有在使用者能看到側邊欄區域時,才能使...。