网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress is_user_logged_in()仅适用于管理员而不适用于其他角色

GG网络技术分享 2025-03-18 16:12 4


问题描述:

I made a custom admin bar that is displayed only for logged in users. But I just noticed that with \"is_user_logged_in()\" function it works only for admins and not contributors or spectators.

The code works for contributors and spectators in specific few pages. I believe it could be because these pages have some code that checks if users are logged in. But I don\'t know.

Here is my PHP and HTML code:

<?php if ( is_user_logged_in() ) : ?>

<a href=\"<?php global $current_user; echo home_url() . \'/members/\' . $current_user->user_login . \'/profile/\'; ?>\">My profile</a>

<a class=\"screen-reader-shortcut\" href=\"<?php echo esc_url( wp_logout_url() ); ?>\"><?php _e(\'Log out\'); ?></a>

<?php else: ?>

<span>Become a member</span>

<a href=\"/wp-login.php\">Log in</a>

<a class=\"cta-small\" href=\"/register\">Get invitation</a>

<?php endif; ?>

Any ideas why it doesn\'t work?

I use Wordpress 5.2.1 and PHP 7.2

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我制作了一个仅为登录用户显示的自定义管理栏。 但我注意到,使用“is_user_logged_in()”函数,它仅适用于管理员,而不适用于贡献者或观众。</ p>

该代码适用于特定几页中的贡献者和观众。 我相信这可能是因为这些页面有一些代码可以检查用户是否已登录。但我不知道。</ p>

这是我的PHP和HTML代码:</ p> \\ n

 &lt;?php if(is_user_logged_in()):?&gt; 

&lt; a href =“&lt;?php global $ current_user; echo home_url()。\'/ members /\'。 $ current_user-&gt; user_login。\'/ profile /\';?&gt;“&gt;我的个人资料&lt; / a&gt;

&lt; a class =”screen-reader-shortcut“href =”&lt;?php echo esc_url(wp_logout_url ());?&gt;“&gt;&lt;?php _e(\'退出\'); ?&gt;&lt; / a&gt;

&lt;?php else:?&gt;

&lt; span&gt;成为会员&lt; / span&gt;

&lt; a href =“/ wp-login.php”&gt;登录&lt; ; / a&gt;

&lt; a class =“cta-small”href =“/ register”&gt;获取邀请&lt; / a&gt;

&lt;?php endif; ?&gt;

</ code> </ pre>

为什么它不起作用的任何想法?</ p>

我使用Wordpress 5.2.1和PHP 7.2 < / p>

</ div>

网友观点:

Problem was with the \\\"WP Fastest Cache\\\" plugin. When turned off error no longer is there.

###

Sometimes the is_user_logged_in() function behaves strangely on template redirects.

Try to use a different condition, replace the first line with this one:

<?php if ( get_current_user_id() != 0 ) : ?>

标签:

提交需求或反馈

Demand feedback