默認(rèn)情況下,WordPress前端和后臺頁面頂部都有一個“管理工具欄”,左側(cè)一般就是站點(diǎn)名稱、評論、新建,右側(cè)就是您好,用戶名稱和頭像。那么我們是否可以在這個管理工具欄中添加一些一二級自定義菜單呢?
其實,我們想要在頂部管理工具欄中添加自定義菜單,只需要使用“admin_bar_menu”鉤子就可以實現(xiàn)。前面boke112百科跟大家分享的『WordPress后臺右上角您好前面怎么增加日期和時間?添加自定義菜單?』一文中介紹了在管理工具欄右側(cè)添加日期和時間,以及一二級自定義菜單,我們只需要刪除該文中的以下代碼:
'parent' => 'top-secondary',
即可讓所添加的日期和時間,以及一二級自定義菜單在工具欄左側(cè)顯示。
比如添加帶鏈接的一級菜單“boke123導(dǎo)航”和二級菜單“免費(fèi)申請收錄”,只需要將以下代碼添加到當(dāng)前主題的functions.php文件中并保存更新文件即可。
/**
* WordPress頂部管理工具欄怎么添加一二級自定義菜單 - boke112百科
* https://boke112.com/post/11933.html
*/
add_action( 'admin_bar_menu', 'boke112_add_custom_menu_adminbar', 500 );
function boke112_add_custom_menu_adminbar( $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu(array(
'id' => 'boke123',
'title' => 'boke123導(dǎo)航',
'href' => 'https://boke123.net/',
'meta' => array('target' => '_blank')
));
$wp_admin_bar->add_menu(array(
'id' => 'boke123-shenqing',
'parent' => 'boke123',
'title' => '免費(fèi)申請收錄',
'href' => 'https://boke123.net/freeweb/',
'meta' => array('target' => '_blank','rel' => 'nofollow')
));
}
說明:
1、meta除了可以添加target外,還可以添加class、rel、onclick、target、title、tabindex等屬性;
2、二級自定義菜單的關(guān)鍵就是第20行代碼中的parent,它的值就是對應(yīng)一級菜單的id;
3、如果也想在前端頂部工具欄右上角自定義菜單,請刪除上述代碼中第7至第9行代碼即可。如果想實現(xiàn)只對管理員有效,那么只需要將上述代碼第7行修改為以下代碼即可:
if ( ! current_user_can( 'manage_options' ) ) {
4、想要將一二級自定義菜單添加到右側(cè)工具欄,只需要在$wp_admin_bar->add_menu(array(數(shù)組里面添加以下代碼即可:
'parent' => 'top-secondary',
具體效果如下圖所示:
除了可以添加一二級自定義菜單外,我們也可以將WordPress后臺常用的菜單添加到工具欄,比如評論、菜單、小工具等,具體代碼如下:
/**
* WordPress頂部管理工具欄怎么添加一二級自定義菜單 - boke112百科
* https://boke112.com/post/11933.html
*/
add_action( 'admin_bar_menu', 'boke112_add_custom_menu_adminbar', 500 );
function boke112_add_custom_menu_adminbar( $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'pinglun',
'group' => null,
'title' => '評論',
'href' => admin_url( '/edit-comments.php' ),
) );
$wp_admin_bar->add_menu( array(
'id' => 'caidan',
'group' => null,
'title' => '菜單',
'href' => admin_url( '/nav-menus.php' ),
) );
$wp_admin_bar->add_menu( array(
'id' => 'xiaogongju',
'group' => null,
'title' => '小工具',
'href' => admin_url( '/widgets.php' ),
) );
}
這樣以后訪問菜單頁面和小工具頁面,就不需要再到“外觀”菜單中點(diǎn)擊了。具體效果如下:
如果你想要在添加的自定義菜單前面也帶有字體圖標(biāo),那么只需要在上述代碼的title值中添加以下代碼即可:
?<span class="ab-icon dashicons-before dashicons-admin-comments"></span>
那么上述代碼第14行評論的title代碼就可以寫成這樣:
'title' => '<span class="ab-icon dashicons-before dashicons-admin-comments"></span>評論',
同樣操作,其他自定義菜單的字體圖標(biāo)只需要修改<span>中的dashicons-admin-comments即可,比如改為鏈接圖標(biāo)就是dashicons-admin-links。
這些圖標(biāo)名稱可前往WordPress官網(wǎng)Dashicons頁面獲得,比如點(diǎn)擊某個圖標(biāo)后,就會看到它所對應(yīng)的名稱,如下圖紅框中的名稱就是dashicons-admin-home。
文章來源:http://www.zghlxwxcb.cn/news/detail-817154.html
來源:https://boke112.com/post/11933.html?文章來源地址http://www.zghlxwxcb.cn/news/detail-817154.html
到了這里,關(guān)于?WordPress頂部管理工具欄怎么添加一二級自定義菜單?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!