WordPress网站后台右上角默认显示的就是“您好,用户名和用户头像”,有些站长想在右上角“您好”的前面增加当前日期和时间,或者添加一些自定义菜单,那么应该怎么做呢?
一、WordPress后台右上角您好前面怎么增加日期和时间?
将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。
/**
* WordPress后台右上角您好前面怎么增加日期和时间 - boke112百科
* https://boke112.com/post/11930.html
*/
add_action( 'admin_bar_menu', 'boke112_add_date_time_adminbar_right', 500 );
function boke112_add_date_time_adminbar_right( WP_Admin_Bar $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'adminbar-date-time',
'parent' => 'top-secondary',
'group' => null,
'title' => date( 'Y-m-d g:i', current_time( 'timestamp', 0 ) ),
'href' => admin_url( '/options-general.php' ),
) );
}
说明:
1、如果也想在前端顶端工具栏右上角显示日期和时间,请删除上述代码中第7至第9行代码即可。
2、显示的日期和时间格式,请直接修改上述代码第14行代码中的’Y-m-d g:i’,具体日期和时间格式可参考『WordPress如何自定义日期和时间格式?附PHP日期和时间格式字符串』一文的格式化字符串。
具体效果如下图所示:
二、WordPress后台右上角您好前面怎么添加自定义菜单?
将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。
/**
* WordPress后台右上角您好前面怎么添加一级自定义菜单 - boke112百科
* https://boke112.com/post/11930.html
*/
add_action( 'admin_bar_menu', 'boke112_add_custom_menu_adminbar_right', 500 );
function boke112_add_custom_menu_adminbar_right( WP_Admin_Bar $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu(array(
'id' => 'boke123',
'parent' => 'top-secondary',
'title' => 'boke123导航',
'href' => 'https://boke123.net/',
'meta' => array('target' => '_blank')
));
}
说明:如果也想在前端顶端工具栏右上角显示日期和时间,请删除上述代码中第7至第9行代码即可。
具体效果如下图所示:
三、WordPress后台右上角您好前面怎么添加一二级自定义菜单?
将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。
/**
* WordPress后台右上角您好前面怎么添加一二级自定义菜单 - boke112百科
* https://boke112.com/post/11930.html
*/
add_action( 'admin_bar_menu', 'boke112_add_custom_menus_adminbar_right', 500 );
function boke112_add_custom_menus_adminbar_right( WP_Admin_Bar $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu(array(
'id' => 'boke123',
'parent' => 'top-secondary',
'title' => 'boke123导航',
'href' => 'https://boke123.net/',
'meta' => array('target' => '_blank')
));
$wp_admin_bar->add_menu(array(
'id' => 'boke123-shenqing',
'parent' => 'boke123',
'title' => '免费申请收录',
'href' => 'https://boke123.net/freeweb/',
'meta' => array('target' => '_blank')
));
}
说明:如果也想在前端顶端工具栏右上角显示日期和时间,请删除上述代码中第7至第9行代码即可。
将鼠标移动到一级菜单上,就会显示出二级菜单,具体效果如下图所示:
总结
想要同时子WordPress后台右上角“您好”前面添加日期和时间,以及自定义一二级菜单,只需要将多添加几个$wp_admin_bar->add_menu代码即可。
比如本文介绍中的日期和时间以及一二级菜单合并后的代码如下:
/**
* WordPress后台右上角您好前面怎么增加日期和时间?添加自定义菜单? - boke112百科
* https://boke112.com/post/11930.html
*/
add_action( 'admin_bar_menu', 'boke112_add_date_time_menu_adminbar_right', 500 );
function boke112_add_date_time_menu_adminbar_right( WP_Admin_Bar $wp_admin_bar ) {
if ( !is_admin() ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'adminbar-date-time',
'parent' => 'top-secondary',
'group' => null,
'title' => date( 'Y-m-d g:i', current_time( 'timestamp', 0 ) ),
'href' => admin_url( '/options-general.php' ),
) );
$wp_admin_bar->add_menu(array(
'id' => 'boke123',
'parent' => 'top-secondary',
'title' => 'boke123导航',
'href' => 'https://boke123.net/',
'meta' => array('target' => '_blank')
));
$wp_admin_bar->add_menu(array(
'id' => 'boke123-shenqing',
'parent' => 'boke123',
'title' => '免费申请收录',
'href' => 'https://boke123.net/freeweb/',
'meta' => array('target' => '_blank')
));
}