內容簡介
在WordPress的「外觀」>「選單」中建立選單並生成對應的HTML清單。
可以自訂元素的類別(class)和ID,方便網頁設計,並可輕鬆加入「跳至內容」的連結,以提供給螢幕閱讀器使用者使用。
使用方式
由於在WordPress管理後台使用時需要先建立選單,然後再在模板檔案中引用,因此盡量使用 WPAdminMenuManager 單例(singleton)類別的靜態方法來處理管理選單。
首先,在管理頁面載入前,呼叫 WPAdminMenuManager 的「createAdminMenu」方法,必填的參數為選單的slug和標題,第三個參數則是其他屬性的雜湊表(hash map),可視為是選單的選項。這個步驟可以讓 WordPress後台的「外觀」>「選單」中出現該選單。
接著可以使用模板檔案或是 WordPress 內容編輯器中的 shortcode 來印出選單。
= PHP
使用模板檔案印出選單,只需要使用 WPAdminMenuManager 類別的「printAdminMenu」方法,必填參數為該管理選單的slug(在建立選單時設定)及一個可選的屬性雜湊表,若想要在不同類別的元素中以不同的樣式印出同一個選單時可使用。
= shortcode
想要透過_shortcode印出選單,可以使用 [admin-menu slug=”%slug%”],可選的屬性可以覆蓋建立選單時的設定。
WPAdminMenuManager還有兩種範本,分別是 header 和 footer ,只需要呼叫 WPAdminMenuManager::createHeaderMenu() 或 WPAdminMenuManager::createFooterMenu(),在必要時使用 WPAdminMenuManager::printHeaderMenu() 或 WPAdminMenuManager::printFooterMenu(),或是使用 shortcode [header-nav] 或 [footer-nav] 即可。
如果嘗試印出還未建立的選單,則不會顯示任何輸出。
管理選單的合法屬性(所有屬性皆為可選的):
“nav”:代表包含所有元素的 nav 元素。應包含一個帶有 class 或 id 屬性的雜湊表,或兩者皆有。
“ul”:代表 ul 元素。應與 “nav” 相同。
“li”:代表 li 元素。應包含一個帶有 class 屬性的雜湊表。
“a”:代表 a 元素。應與 “li” 相同。
“subnav”:代表多級選單的子 ul 元素。應與 “li” 相同。
“subitem”:代表多級選單的子 li 元素。應與 “li” 相同。
“sublink”:代表多級選單的子 a 元素。應與 “li” 相同。
“parent-link”:代表包含子選單的頂層 li 元素的子元素 a。應與 “li” 相同。
“skip-to-content”:自動加入「跳至內容」的連結。應設定為一個字串,表示此連結要前往的錨點(不包含#),沒有設置的話就不會顯示「跳至內容」連結。
簡碼屬性也可以作為這些:
nav-class
nav-id
ul-class
ul-id
li-class
a-class
subnav-class
subitem-class
sublink-class
parent-link-class
skip-to-content
範例
// functions.php
declare( stric
外掛標籤
開發者團隊
原文外掛簡介
Creates menu in WordPress’s Appearances -> Menus & generates list HTML from it.
Allows custom-set classes & IDs for elements for easier styling & has easy way to automatically add “Skip to Content” link for screen-reader users.
Usage
Since admin menus need to be created before initialization to work in the WordPress admin & used later in template files to print, admin menu objects are handled through static methods in the WPAdminMenuManager singleton class.
1st, before admin loads, call the “createAdminMenu” method on WPAdminMenuManager with a slug & title as the 1st 2 mandatory, & a hash map o’ other attributes as an optional 3rd argument. This will make the menu appear under the given name in Appearances -> Menus in the WordPress admin.
Then you can print the menu through PHP in template files or shortcodes in WordPress content editors.
= PHP
To use through PHP, just call the “printAdminMenu” method on the WPAdminMenuManager class with the mandatory argument o’ the slug representing the admin you want to print ( given when creating the menu earlier ) & an optional hash map o’ attributes to o’erride the ones given when creating the menu, if you want to print the same menu in different places with different element classes, for instance.
= Shortcodes
To print through shortcodes, just use the shortcode [admin-menu slug=”%slug%”], with optional extra attributes to o’erride the attributes given when creating the menu.
WPAdminMenuManager also has 2 templates for easier creation o’ admin menus, a header & footer template. Just call WPAdminMenuManager::createHeaderMenu() or WPAdminMenuManager::createFooterMenu() respectively to create them & either call WPAdminMenuManager::printHeaderMenu() or WPAdminMenuManager::printFooterMenu() or use the shortcodes [header-nav] or [footer-nav].
If you try to print a menu that hasn’t been created yet, it will print nothing.
Valid attributes for admin menus ( all are optional ):
“nav”: represents the nav element that holds everything. Should hold a hash map with either a class or id attribute, or both.
“ul”: represents ul element. Should hold the same as “nav”.
“li”: represents li element. Should hold a hash map with a class attribute.
“a”: represents a element. Should hold the same as “li”.
“subnav”: represents child ul elements for multilevel menus. Should hold the same as “li”.
“subitem”: represents child li elements for multilevel menus. Should hold the same as “li”.
“sublink”: represents child a elements for multilevel menus. Should hold the same as “li”.
“parent-link”: represents a elements in top-level li elements that hold child navs. Should hold the same as “li”.
“skip-to-content”: automatically adds “skip to content” link. Should be a string that will be the anchor ( without the # ) that the “skip to content” link should go to. If not added, “skip to content” link will not be added.
Shortcode attributes that act as these:
nav-class
nav-id
ul-class
ul-id
li-class
a-class
subnav-class
subitem-class
sublink-class
parent-link-class
skip-to-content
Example
// functions.php
declare( strict_types = 1 );
namespace MyTheme
{
use WaughJ\WPAdminMenuManager\WPAdminMenumanager;
// Make sure this is initialized early,
// so WordPress Admin knows that this menu is set up.
WPAdminMenuManager::createAdminMenu
(
'extra-menu',
'Extra Menu',
[
'nav' =>
[
'class' => 'extra-menu-nav',
'id' => 'extra-menu-nav'
],
'ul' =>
[
'class' => 'extra-menu-list',
'id' => 'extra-menu-list'
],
'li' =>
[
'class' => 'extra-menu-item'
],
'a' =>
[
'class' => 'extra-menu-link'
],
'subnav' =>
[
'class' => 'extra-menu-subnav'
],
'subitem' =>
[
'class' => 'extra-menu-subitem'
],
'sublink' =>
[
'class' => 'extra-menu-sublink'
],
'parent-link' =>
[
'class' => 'extra-menu-parent-link'
],
'skip-to-content' => 'top'
]
);
}
// inc/header.php
