Hi,
I am trying to show a stamp using conditional display based on the post author role on the website,
for example : admin, dealer, editor , or any authorized users will have a stamp after the user account on a post, where other users or visitors won't, this is really important to the website concept.
I am trying to do it using wpv-post-user, role and condition display,
attached snapshots,
any help will be appreciated,
thanks
PS: I will be creating different user forms, where each will assign any registration to different roles, for example: a user form for editiors, when a user sign up with that form will be assigned an editor role.
thanks again
Hi,
You are right, it is not possible within Views built-in shortcodes, but you can create another custom shortcode to get the user role of post author, then use it into [wpv-conditional] shortcode, for example there is a solution in another similar thread:
https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/#post-543582
For your reference
Thank you so much ! that worked,
one more question, I am entering the conditions manually, like :
[wpv-conditional if="'[user_role_func]' eq 'administrator'"]
<!-- Verification Stamp code -->
// content
<!-- Verification Stamp code -->
[/wpv-conditional]
a) how would I modify the condition to include multiple roles, comma spattered ? or using OR
[wpv-conditional if="'[user_role_func]' eq 'administrator' OR '[user_role_func]' eq 'editior' OR '[user_role_func]' eq 'dealer' "]
<!-- Verification Stamp code -->
// content
<!-- Verification Stamp code -->
[/wpv-conditional]
b) what should be the non-logged-in or visitors roles be ?
Thanks a lot
a) how would I modify the condition to include multiple roles, comma spattered ? or using OR
Yes, you can use OR logic as the condition, for example:
[wpv-conditional if="('[user_role_func]' eq 'administrator') OR ('[user_role_func]' eq 'editior') OR ('[user_role_func]' eq 'dealer') "]
<!-- Verification Stamp code -->
// content
<!-- Verification Stamp code -->
[/wpv-conditional]
More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#syntax
Boolean operators: AND, OR, NOT
b) what should be the non-logged-in or visitors roles be ?
I tested it in my localhost, if the post author is a non-logged-in or visitors, that shortcode [user_role_func] will trigger PHP errors, but you can modify the PHP codes as below:
function get_author_role()
{
global $authordata;
if($authordata == false){
return 'visitor';
}
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
add_shortcode( 'user_role_func', 'get_author_role' );
Then check if it is visitor like this:
[wpv-conditional if="'[user_role_func]' eq 'visitor'"]
<!-- Verification Stamp code -->
// content
<!-- Verification Stamp code -->
[/wpv-conditional]
Thank you Luo, that worked just like perfect , except for minor issue, it works great in Layouts but in a Search view or View Slider, it shows an empty space instead, I added snapshots, if there is no way around, I can just assign a class for that div and using custom js to add it manually.
I was able to solve it in view using another way since it shows an empty space for visitors, and since I will be triggering all other roles, by using :
[wpv-conditional if="'[wpv-post-author]' eq ''"]
visitor
[/wpv-conditional]
if there is a better stable way using php I would like it, otherwise I am so much thankful for the help Luo!
Since your screenshot isn't in English, I am not sure which one is the "empty space for visitors", where is it from?
It works fine in my localhost, if you still need assistance for it, please try these:
1) deactivate other plugins and switch to wordpress default theme, and test again
2) If the problem still persists, please provide a database dump file (ZIP file) of your website in below private detail box, I need to test and debug it in my localhost, thanks
Hi Luo, Thank you for the reply,and sorry for the confusion, I think I have solved using the following :
[wpv-conditional if="'[wpv-post-author]' eq ''"]
visitor
[/wpv-conditional]
and since I will try to track all the other roles, this should and has been working fine.
thanks again