Login/Logout 菜单栏中的字段不合适且无法正常工作 (Wordpress)

Login/Logout field in menu bar is out of place and not functioning (Wordpress)

过去两天我一直在努力学习如何在我网站的顶部菜单栏中添加一个漂亮的 login/logout/register 字段。我最近才开始熟悉编辑器。我在 Whosebug 中找到了这段代码。我喜欢它的外观,希望能稍微改进一下。

    /* 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;
}

我想要做的是让登录字段区域在 top/main 菜单栏的中间水平居中。如您所见,它目前一直在向右延伸,并向外延伸。当我使用它时 - 它确实让我登录,但在登录时不会切换到 "logout" 选项。

如果有人在这方面有任何关于如何添加或修改我的起始代码的知识,我将不胜感激。最好的问候- Cypher。

站点:cypherbeats.com 主题:贾维斯(最新) 代码已添加到 functions.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/ Then create a login link which is only visible to logged out users and a logout link that is only visible to logged in users. You could then write some javascript which causes the login form to popup in a lightbox when you press the login button. I use this approach at https://ptofchoice.com.au/