0

I have a field in my user profile of byline (field_byline). I'd like to call this in the node template so that the byline is included next to the author's name (e.g. Posted by Mr. Rogers | Sweater Entrepreneur) I kind of have it working, but not quite there yet. I'm using Drupal 7.10

Right now, I have this in my template.php

global $user;
  $user = user_load($user->uid);
  $bylines = field_get_items('user', $user, 'field_byline');
  if ($bylines) {
    $vars['field_byline'] = $bylines[0]['value'];
  }

}

And this in my node.tpl.php

if (isset($field_byline) && !empty($field_byline)) :
  echo '<span class="byline">| ' . $field_byline.'</span>';
endif;

But, my problem is this only calls the correct byline while viewing the individual node. It breaks on the front page when all the nodes are looped out and only displays the byline of the first user, not the one that wrote the post.

Any help or direction would be much appreciated!

3

global $user is that of the currently logged in user, not specifically the owner of any specific content.

So what your code was doing was just looping through printing your byline, eg, admin, eg uid 1. What you need to do is grab the uid from the specific user's content, user_load(THAT-UID) into a variable, and then use that to get the byline into vars[] in hook_preprocess_node() so it goes to the specific node in question, not the page it is printed on.

(in D6 at least, still learning D7 but I am sure it's pretty similar)

Addition/Edit: In D6, you can get the uid of the node author like this:

function hook_preprocess_node(&$vars) {

  $node=$vars['node'];
  $uid=$node->uid;

  $byline= "whatever you need to do to get it with $uid"

  $vars['byline']=$byline;

}

just change hook to your theme or module name and you should be on your way. You can now use the byline variable in your node.tpl.php (barring any typos or brainfades of mine above :)

  • Ah I see. That makes sense, but would you know how to get user_load(THAT-UID)? – btotheg Dec 8 '11 at 23:20
  • The uid you need is $node->uid. It is available within your node.tpl.php as $uid, so you could do it all in there, which is ugly, or you can create a preprocess_node() hook that looks at vars['node'] and grabs it off there, eg $node=$vars['node']; – Jimajamma Dec 9 '11 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.