How do you print a custom profile value? I'll be adding this to comment.tpl.php

Tried '$location', '$profile->location', "$profileobject->location'. Nada.

Comments

yelvington’s picture

You'll need to get the username or uid from the $comment object, then use that to load the user record with user_load(). The resulting object will have the profile information, including your custom fields. A bit of examination with print_r() will help you understand it.

czarphanguye’s picture

Thanks for the reply, yet I have no idea what you just said.

Regards,

Czar

nevets’s picture

To follow up on yelvington's suggestion, as code this woudl be

$account = user_load(array('uid' => $comment->uid));
profile_load($account);
// Now you should be able to access the profile fields
// Not sure what the fields are, use the following to see all the user & profile fields
print '<pre>' . print_r($account, TRUE) . '</pre>';
Anonymous’s picture

How do you figure out when you need to do load stuff like 'user_load()' and what the actual command is?
For example, I need to do basically the same thing but for a taxonomy term. I want to be able to display Tags in the teaser. I tried things like $node->taxonomy->name...then realized it's in an array so tried some other stuff like $node->taxonomy[30]['name']. Eventually figured out taxonomy wasn't the array name...problem was I couldn't figure out what the array was called as it wasn't displayed in print_r. Unless I was doing something wrong...which wouldn't be a surprise. Is there an easy way to figure out what variables and what not you need to display certain fields?

nevets’s picture

First off, some of it is just learning since there is no single "solution", that said here are some tips to get you started. A good starting point is the PHPtemplate section of the handbook. In particular check out the *.tpl.php sections which show the standard variables to a given template file. Some are strings, some are objects. You can always use

<?php print '<pre>' . print_r($variable, TRUE) . '</pre>'; ?>

to see the details of one of the variables. You will wan to replace $variable with the actual variable name (example: $comment for comment.tpl.php).

As for the taxonomy terms, $node->taxonomy holds the terms associated with a given node. $node->taxonomy is an array of taxonomy terms (as objects) and you can access a given element with $term = $node->taxonomy[30] and a particular element $node->taxonomy[30]->name. You can also loop through the array with something like

<?php
foreach ( $node->taxonomy as $term ) {
// Do something with the term, here we print the
// term to see what the object holds
print '<pre>' . print_r($term, TRUE) . '</pre>';
}
?>
alxbkh’s picture

<?php
foreach ( $node->taxonomy as $term ) {

print '<div class="person-name">'.l($term->name, 'node/'. $term->tid).'</div>';
}


?>

thanks a lot for this topic, it helps to improve my site too)