WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional Display Based on Role (3 posts)

  1. noelgreen
    Member
    Posted 2 years ago #

    I want to display something different at the bottom of a post depending on what role the post's author was/is.

    For example... and this doesn't work... but...

    <?php
    if (the_author_meta('user_level') == 'Editor'): ?>
    <p>Boogie Time</p>
    <?php else: ?>
    <p>Time to Boogie</p>
    <?php endif; ?>

    Anyone know how to do this?

  2. noelgreen
    Member
    Posted 2 years ago #

    Got it!

    Step 1) Download the User Role Editor plugin here.

    Step 2) Set a user role with a specific level (i.e., "level 5")

    Step 3) Add the following code where you want to display the conditional text.

    <?php $authorrole = esc_attr(get_the_author_meta('user_level') );
    	if ($authorrole == 5) { ?>
    		<p>This displays if the user is in the group set to "level 5"</p>
    	<?php } else {?>
    		<p>If the user is in any group other than the one set to "level 5" this displays.</p>
    <?php } ?>

    Simply change the # to the level you want to use as the level. Alternatively you should be able to change the function so that if a user is greater than a certain level it will display (though I've not tested this)... like so...

    <?php $authorrole = esc_attr(get_the_author_meta('user_level') );
    	if ($authorrole > 2) { ?>
    		<p>This displays if the user is in the group set higher than "level 2"</p>
    	<?php } else {?>
    		<p>If the user is in any group other than the one set lower than "level 2" this displays.</p>
    <?php } ?>

    Hope this helps others!

  3. noelgreen
    Member
    Posted 2 years ago #

    Or, let's say you want to get the information for the current user and display something if they're at or above a level.

    That would be done like this...

    <?php
    global $current_user;
    get_currentuserinfo();
    	$authorrole = esc_attr($current_user->user_level );
    	if ($authorrole == 5) { ?>
    
    Do something.
    
    	<?php } else {?>
    
    Do something else.
    
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic