Custom user profile page with (x) latest weblog entries of that user

So this PHP snippet is actually a combination of a few tricks. The first trick is overriding the user.profile theme output to display profile information however you want, based on the popular overriding user profile thread. The other trick is another PHP snippet on this book page which lets you pull up the last (x) weblog entries.

You'll need PHPTemplate in order for this to work.

First you need to catch the profile.module theme output.
Create a file called template.php in your theme directory and include the following php code:

<?php
/**
* Catch the theme_profile_profile function, and redirect through the template api
*/

function phptemplate_user_profile($user, $fields = array()) {
 
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
 
return _phptemplate_callback('user_profile', array('user' => $user, 'fields' => $fields));
}
?>

This dumps all the variables out to phptemplate which will let you customize it. Let's say I have added the custom profile fields, "Name", "Age", and "Bio" using the profile.module. In addition, I want to display the user's picture and the last (x) blog entries by that user.

Create a file called user_profile.tpl.php in your theme directory and include the following code:

<?php  if ($user->picture): ?>
<img  src="/ <?php print $user->picture ?>">
<?php endif; ?>

<div class="fields"><b>Name: </b><?php print $user->profile_name ?></div>
<div class="fields"><b>Age: </b><?php print $user->profile_age ?></div>
<div class="fields"><b>Bio: </b><?php print $user->profile_bio ?></div>

<?php
unset($output);
/**
* This php snippet displays the 10 most recent weblog entries by a specific user with
* teaser & info.
*
* To increase/decrease the number of weblogs listed
* change the $listlength field to suit.
*
* Works with drupal 4.6
*/
 
$listlength=10;
 
$userid=$user->uid;
 
$result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 AND n.uid = $userid ORDER BY n.created ASC"), variable_get('default_nodes_main', $listlength));
  while (
$node = db_fetch_object($result1)) {
   
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }

print
$output;

?>

Further Usage:
You could create a myspace.com profile type site if you customize the page even further to pull up all relevant content of a specific user and display it on the user's profile page.
In other words, pull up the latest songs, videos, or images that a user has submitted. You could do this with a modified taxonomy code snippet and include it in your user_profile.tpl.php file. You'd need to do a database query to find all the nodes that match the taxonomy you want and were authored by the user who's profile you are looking at.

 
 

Drupal is a registered trademark of Dries Buytaert.