在过去的两天里,我一直在努力学习如何在我的网站的顶部菜单栏中添加一个漂亮的登录/注销/注册字段。我最近才开始熟悉编辑。我在stackoverflow找到了这个代码。我喜欢它的样子,我希望能用粗花呢装饰一下。
/* Custom Login Menu Field */
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
// start buffering
ob_start();
// this is the actual form function
wp_login_form($args); // $args optional see below ..
// get buffering
$loginoutform = ob_get_contents();
// clean buffering
ob_end_clean();
$items .= $loginoutform ; // concatenate buffering with items ...
return $items;
}
我想做的是让登录字段区域水平居中在顶部/主菜单栏的中间。正如你所看到的,它目前一直向右延伸,并延伸了整个过程。当我使用它时,它确实会让我登录,但在登录时不会切换到"注销"选项。
如果有人对我如何添加或修改我的入门代码有任何了解,我将不胜感激。向Cypher致以最良好的问候。
网站:cypherbeats.com主题:贾维斯(最新)代码已添加到函数中。php
在撰写本文时,我看不到您提到的登录栏。所以不能说你的代码是否有效。
在您的代码中,您可以使用函数is_user_logged_in()
作为条件来检查访问者是否已登录。因此,您可以用它来决定是输出登录代码还是注销代码。出现wp_loginout()
功能以显示注销按钮。所以像这样的东西可以工作:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
// start buffering
ob_start();
// this is the actual form function
if( !is_user_logged_in() )
wp_login_form($args); // $args optional see below ..
else
wp_loginout();
// get buffering
$loginoutform = ob_get_contents();
// clean buffering
ob_end_clean();
$items .= $loginoutform ; // concatenate buffering with items ...
return $items;
}
警告我还没有测试过这个,只是建议你开始。
这里有一种可能很方便的替代方法。首先安装此插件:https://wordpress.org/plugins/nav-menu-roles/然后创建一个仅对已注销用户可见的登录链接和一个只对已登录用户可见的注销链接。然后,您可以编写一些javascript,当您按下登录按钮时,会在灯箱中弹出登录表单。我在https://ptofchoice.com.au/