I have pieced together several pieces of code to create a list of recent blog entries posted by the current user being view. You can use this without using views or using a module. It's rather simple. This is to be used in your user override template "user-profile.tpl.php". You can paste right at the bottom of the file.

<?php
$nlimit = 5;
$query= "SELECT n.created, n.title, n.nid, n.created FROM {node} n WHERE n.uid = %d AND n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($query), $account->uid, 0, $nlimit);
while ($node = db_fetch_object($result)) {
     	print '<div><ul><a href="';
        print '../' . drupal_get_path_alias('node/'.$node -> nid);
        print '">' . $node->title . "</a>\n&nbsp;-&nbsp;";
		print date('F d,y', $node->created) . '</ul></div>';
}
?>

In the code above I query the database for all content where the user on the profile page being viewed is the author and where the content type is a blog entry. I then sort the created date by descending, giving me the most recently created blog first and so on. I then limit the number of entries shown to the number I defined first with $nlimit. Here I have limited to 5. You can change the number to whatever you want. Next I have it print the fields I want from the content for each entry, in this case a linked title for the blog entry, which I called up the blogs url alias instead of using the nodes ID, either will work, but this way is prettier, and the blogs created date, which I formatted. It looks nice and pretty.

This took me awhile to find the right combination of code to get it right. I wanted to share this, because people have asked for and looked for something to do just this, including me. After searching and searching I finally stumbled upon a few things that worked in a way I sort of wanted but not fully. I pieced them together and voila!

It is very simple to implement even if you haven't customized your user-profile.tpl.php file. Just paste this code at the bottom of the file and it works. No custom fields or anything you really have to change. If you want to format it differently that is up to you. It turned out nice for me.

Here's an example of how this looks:
http://www.flickr.com/photos/48785425@N07/4468915462

Comments

ExJx’s picture

I forgot to add this earlier, but if someone can take this code and add code to have it print " 'Username' has not posted any Blog Entries Yet" when the user has no blog entries it would be nice. I am currently working on it but can't figure it out.

ExJx’s picture

$nlimit = 5;
$query= "SELECT n.created, n.title, n.nid, n.created FROM {node} n WHERE n.uid = %d AND n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($query), $account->uid, 0, $nlimit);
$result2 = db_query_range(db_rewrite_sql($query), $account->uid, 0, $nlimit);
if (db_fetch_object($result2) == False) {
print check_plain($account->profile_name) . ' has no blog entries';
}
else {
while ($node = db_fetch_object($result)) {
				print '<div class="tfont"><ul><h5><a href="';
				print '../' . drupal_get_path_alias('node/'.$node -> nid);
				print '">' . $node->title . "</a>\n&nbsp;-&nbsp;";
				print date('F d,y', $node->created) . '</h5></ul></div>';
				
			}
		print '<div class="alignright"><a href ="../blogs/' . check_plain($account->name) . '"  class="tfont">More ></a></div>';
}

Okay so I added an if statement. Took me a minute but I was able to correct the problem. Now it prints " 'Username' has no blog entires" when the user has none. I also added a more link to the else statement so if there are blog entries the user view can go to the user being viewed blog page to see more blog entires. This also makes it so it doesn't print the more link if there are no entries. So have fun with this very nice snippet that seems to be what everyone is looking for.

Now it just needs a check to see if there are more than 5 entries or not and if there are not hide the more link, but this will do very well for now.

jyiping’s picture

Terrific!!!!!!!!!!!!!!!!!!! it works great!!!
I just try to add a string for Body, but I'm not able to reach my target.
What I want to show is a real "blog style" page in user profile page. I want to show blog's entry Title, body, add comment. But when I use variable like " print $node->body ", it doesn't show anything in my page.

ExJx’s picture

Thanks for the reply. I am not sure how to do this, but you may want to look for the code to pull the teasers and I am not sure about add comment. I am sure there is away. There is an alternative way to do this and I am going to post it later this week. It uses views 2 and can be added with a block. So I now you can show full blog and all the extras with views. the add comment I am not sure. I want to check into it myself.
I used the new way to show the latest stories with a more link to a page with all users stories and the page shows the teasers and everything, but the block on the user profile should do that too. sorry I couldn't help you way back when, but if you are still interested hit me up.

Jumoke’s picture

check_plain($account->name) doesn't give me the username. It gives me the realname.
When i click "More >" I get mysite.com/blogs/firstname lastname

instead of

mysite.com/blogs/username

What's the fix?

ExJx’s picture

There is another variable to use I think it's $account->username not sure though you'll have to check the Drupal docs.

ExJx’s picture

I will be posting a new tutorial on how to do this with views. It looks way better and more custimizable. I will post link here and who ever needs it can find it. Aand yes I just found out you can add add comment link in views.

Caderial’s picture

Can you please post that link to your Views solution to this?