菜单栏中的“登录/注销"字段不正确且无法正常使用(Wordpress) [英] Login/Logout field in menu bar is out of place and not functioning (Wordpress)

查看:55
本文介绍了菜单栏中的“登录/注销"字段不正确且无法正常使用(Wordpress)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两天,我一直在尝试学习如何在网站的顶部菜单栏中添加一个不错的登录/注销/注册字段.我刚刚开始熟悉编辑器.我在stackoverflow中将这段代码挖了过来.我喜欢它的外观,希望对它有所了解.

The past two days I have been trying to learn how to add a nice login/logout/register field into the top menu bar of my site. I just recently began familiarizing myself with editor. I dug this code up here in stackoverflow. I like the way it looks and I'm hoping to tweek it a bit.

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

我想做的是使登录字段区域在顶部/主菜单栏的中间水平居中.如您所见,它目前一直在向右延伸,并扩展了整个过程.当我使用它时-它确实可以登录,但登录后不会切换到注销"选项.

What I would like to do is have the login field area centered horizontally in the middle of the top/main menu bar. As you can see it is currently all the way to the right and stretches out the whole thing. When I use it- it does log me in, but does not switch to a "logout" option when signed in.

如果任何人在这方面对我如何添加或修改启动程序代码有任何了解,我将不胜感激.最好的问候-密码.

If anyone has any knowledge in this area on how I could add or modify my starter code I would be very grateful. Best regards- Cypher.

网站:cypherbeats.com 主题:贾维斯(最新) 代码已添加到functions.php

Site: cypherbeats.com Theme: Jarvis (latest) Code was added into functions.php

推荐答案

在撰写本文时,我看不到您所说的登录栏.因此无法说出您的代码是否有效.

At time of writing I cannot see this login bar you speak of. So cannot say whether your code works.

在您的代码中,可以使用功能is_user_logged_in()作为条件来检查访问者是否已登录.因此,可以使用它来决定是输出登录代码还是注销代码.出现wp_loginout()功能以显示注销按钮.所以像这样的工作:

In your code you can use the function is_user_logged_in() as a conditional to check whether the visitor is logged in. Therefore you can use it to decide whether to output the login or logout code. The wp_loginout() function appears to display a logout button. So something like this coud work:

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

警告,我尚未对此进行测试,但这只是一个建议,供您开始使用.

Warning I haven't tested this and is just a suggestion to start you off.

这是一种可能很方便的替代方法.首先安装此插件: https://wordpress.org/plugins/nav-menu-roles/然后创建仅对注销用户可见的登录链接和仅对登录用户可见的注销链接.然后,您可以编写一些JavaScript,从而在您按下登录按钮时使登录表单在灯箱中弹出.我在 https://ptofchoice.com.au/

Here is an alternative approach that might be handy. First install this plugin: 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/

这篇关于菜单栏中的“登录/注销"字段不正确且无法正常使用(Wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆