I have a WordPress site with “Sign In”option right side top: http://www.chicago-plastic-surgeons.com/

I want that when login in my WordPress site the then display a “Log Out” option only and “Sign In” would be disappeared like this: http://www.chicago-plastic-surgeons.com/events/

This is my code:

<div class="fix floatleft sign_in">
<a href="http://www.chicago-plastic-surgeons.com/wp-login.php">Sign In</a>
<a href="http://www.chicago-plastic-surgeons.com/wp-login.php?action=logout&  amp;redirect_to=http%3A%2F%2Fwww.chicago-plastic-surgeons.com&amp;_wpnonce=6049e0b736">Log out</a>
</div>

How could I fix it? Thank you.

N.B. Recently I have changed my site home page. So, I have already created and customized wp-login.php file in WordPress directory. I just want to use link form there which I mentioned in my code.

有帮助吗?

解决方案

It's relatively simple to achieve that, you need to use the is_user_logged_in() function, as follows:

<div class="fix floatleft sign_in">
    <?php if(is_user_logged_in()) : ?>
        <a href="http://www.chicago-plastic-surgeons.com/wp-login.php?action=logout&amp;redirect_to=http%3A%2F%2Fwww.chicago-plastic-surgeons.com&amp;_wpnonce=6049e0b736">Log out</a>
    <?php else : ?>
        <a href="http://www.chicago-plastic-surgeons.com/wp-login.php">Sign In</a>
    <?php endif; ?>
</div>

The code asks whether the current user is logged in, and if they are show the log out button, if they're not logged in, it will show the log in button.

You can find out more information about the is_user_logged_in function on the WordPress Codex.

许可以下: CC-BY-SA归因
scroll top