By prattboy on
Hey all. I want to theme a node slightly different depending on the role of the node's author. Specifically, if the author is of a particular role, I want to display some additional content. How do I accomplish this? I can get the author's uid... and I've tested for the role of the currently logged in user, but I'm stumped. Any code snippets that someone can share?
Thanks.
Comments
The problem is a user's
The problem is a user's roles are stored in an array, so it's not a simple a = b situation.
Post the following snippet in a PHP page in your site to see what I mean:
Toward the bottom will be the roles array. So, you can poll through the roles a user has and if a certain role is detected then theme the page differently:
Obviously, you'll need to modify this... but the premise works.
----------------------
Current Drupal project: http://www.ubercart.org
That works great with one
That works great with one exception: when using it, it would log the user in as the author of the page if they viewed a page with that code in it. I suspected that it was due to me calling global $user; earlier in the document.
So I just changed $user in the snippet you gave to something else and it worked a-okay. Any concerns I should have with doing that?
Thanks for the quick response; I appreciate it.
No worries, you were right
No worries, you were right to change $user from my snip to something else. : )
----------------------
Current Drupal project: http://www.ubercart.org
role printed next to author
Hi there,
I can see your code is what i need...but not sure how to modify it and where to put it.
if say i wanted to print the role next to the author on a tracker page what would i do exactly?
thanks
Unfortunately, you can't
Unfortunately, you can't really add it to existing pages easily like that. One thing you might try is using the Views module to override the tracker page. Doing that lets you add extra fields and things, and it might just work for user role.
----------------------
Drupal by Wombats | Current Drupal project: http://www.ubercart.org
Actually you can in node.tpl.php
Your existing node.tpl.php will have a section something like
We are going to change this to
This uses the original code from the phptemplate engine add adds the role.
For this to work you will need to add the snippet to the beginning of node.tpl.php and use that to set $role to the information you want printed after (in the example) the users name
Subscribe
link