Kenshi
Kenshi

Reputation: 187

How to display different links for logged in and logged out users?

When a user is not logged in I am trying to show

Support || Log In

When they are logged out it should say

Support || Log Out 

Here is the code I tried to use to get this to work

    <div class="fr">
            <ul class="rss">

                <li><a href="http:/jfdfjdf.com/wp-login.php">Support</a></li>
                <li><?php if (is_user_logged_in() ) { echo " <a href=" . wp_logout_url() . " title=\"Logout\">Logout</a>";}?></li>
                <li><?php else if (!is_user_logged_in() ) { echo " <a href="fdjdjfd.com" title=\"Logout\">Member Login</a>";}?></li>
            </ul>
        </div>

But it is not working can anybody help me out?

Upvotes: 0

Views: 8806

Answers (8)

mevrick
mevrick

Reputation: 1045

Apart from coding you can always use a plugin called Nav Menu Roles

Screenshot

Upvotes: 1

wDKidd
wDKidd

Reputation: 1

Add this to functions.php in your theme or create a plugin. Change 'menu' to the menu location eg. 'primary-menu' in your theme. Change 'logged-in' to the name of the logged in menu and logged out respectively.

<?php
  function my_wp_nav_menu_args( $args = '' ) {

    if( is_user_logged_in() ) { 
        $args['menu'] = 'logged-in';
    } else { 
        $args['menu'] = 'logged-out';
    } 
        return $args;
    }
    add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>

referenced from: WPBeginner

Upvotes: 0

freshmaker8
freshmaker8

Reputation: 11

Another way to display content for users with a shortcode. Post this into functions.php

// When member is logged in [memberin]

add_shortcode( 'memberin', 'member_check_shortcode' ); 
function member_check_shortcode( $atts, $content = null ) {
function member_check_shortcode( $atts, $content = null ) {
  if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}

// When member is logged out [memberout]

add_shortcode( 'memberout', 'member_check_shortcode_out' );
function member_check_shortcode_out( $atts, $content = null ) {
  if (!is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}

Upvotes: 1

George Cummins
George Cummins

Reputation: 28906

Your code has a syntax error:

<li><?php else if (!is_user_logged_in() ) { echo " <a href="http://example.com/wp-login.php" title=\"Logout\">Member Login</a>";}?></li>

You should escape the double-quotes:

<li><?php else if (!is_user_logged_in() ) { echo " <a href=\"http://example.com/wp-login.php\" title=\"Logout\">Member Login</a>";}?></li>

Upvotes: 3

karevn
karevn

Reputation: 575

Use this code:

<div class="fr">
  <ul class="rss">
    <li><a href="http://example.com/wp-login.php">Support</a></li>
    <li>
      <?php if (is_user_logged_in() ): ?>
        <a href="<?php echo wp_logout_url() ?>" title="Logout">Logout</a>
      <?php else: ?> 
        <a href="http://example.com/wp-login.php" title="Logout">Member Login</a>
      <?php endif ?>
    </li>
   </ul>
</div>

Your mistake is that you should not insert anything between closing } and else keyword. Also, in templates, oldschool if, while, foreach form should be used - see above.

Upvotes: 1

Robert
Robert

Reputation: 8767

Not sure about the result of your function, but to start you were not escaping properly. Secondly, why not just use one li to house the correct link as follows:

<div class="fr">             
    <ul class="rss">                  
        <li><a href="http://example.com/go/wp-login.php">Support</a></li>                 
        <li><?php if (is_user_logged_in() ) { 
                echo " <a href=\"" . wp_logout_url() . "\" title=\"Logout\">Logout</a>";
            }else{ 
                echo " <a href=\"http://example.com/" title=\"Login\">Member Login</a>";
            } ?>
        </li>             
    </ul>         
</div> 

Upvotes: 2

rcravens
rcravens

Reputation: 8388

What about the following?

<div class="fr">
    <ul class="rss">
        <li><a href="http://example.com/wp-login.php">Support</a></li>
        <?php if (is_user_logged_in() ) { echo " <li><a href=" . wp_logout_url() . " title=\"Logout\">Logout</a></li>";}?>
        <?php else if (!is_user_logged_in() ) { echo " <li><a href="http://example.com/wp-login.php" title=\"Logout\">Member Login</a></li>";}?>
    </ul>
</div>

I moved the php if statements so you don't get empty li elements. You should still do the escaping that others have noticed.

Upvotes: 0

Kratz
Kratz

Reputation: 4340

Have you tried changing ?php else if (!is_user_logged_in() ) to just ?php if (!is_user_logged_in() ) ?

Upvotes: 0

Related Questions