Can anyone help me understand how I would create a block that would display other nodes by the same author as the current node?

Comments

pwolanin’s picture

Did you look through the block snippets? This one (though for 4.6) could be a starting point: http://drupal.org/node/63965

Here's the general sense of what you need to do:

parse out the path to find the nid, and then call node_load($nid) (see, for example: node_menu()).

From the $node object, get $node->uid to find the author.

Use something like the snippet above to select x number of the most recent posts by that author.

Display the titles as you desire (use the l() function to make links to them)

(See also the code from the tracker page: tracker_page() )

---
Work: BioRAFT

mmahoney’s picture

pwolanin, I could kiss you! I've been struggling with that, and I got it, thanks to you.

Here's the code that worked for me:

<?php

$nlimit = 15; //max nodes to show, edit this value
$node_type = 'story'; //type of node to display in this block

if (arg(0) == 'node' && is_numeric(arg(1))) { 
	$output = null;
	$nid = arg(1);
	$node = node_load(array('nid' => $nid));
	$author = $node->uid;
	$result = db_query("SELECT n.created, n.title, n.nid, n.status, n.type
	FROM node n
	WHERE n.uid = $author AND n.status = 1 AND n.type = '$node_type'
	ORDER BY n.created DESC
	LIMIT $nlimit");
	if (db_num_rows($result)) {
		$output = '<ul id="authored_nodes" class="menu">';
		while ($obj = db_fetch_object($result)) {
			$output .= '<li class="leaf">';
			$output .= l(($obj->title), "node/".$obj->nid);
			$output .= '</li>';
		}
		$output .= '</ul>';
	}
	return $output;
}
?>
jaks1970’s picture

This also works in Drupal 5.0. I just tested it and it works great. :)

How would I add a custom field to this snippet? I want to have title and subtitle included in this. It would be cool to add something like this to the top "All stories by USERX".

I'm not so good with PHP. I've tried playing with the code but all I get is errors or just the title.

Thanks in advance.

mmahoney’s picture

You'd have to change the query to a JOIN to get the name for $author in the users table, then you could just add that in h2 tags at the beginning of the $output variable.

patchak’s picture

Another question here,

How would I display the teasers of the nodes, and not a list for this? Thanks

Andrzej7’s picture

Thank for a nice block, but it doesn't work with dbase prefix.

I fixed it manually.

But still a great work :-)

Anonymous’s picture

This code works fine for individual nodes but I'd like to display such a block on the user's blogs pages. The visitor could see a list of recent blog entries posted by the author of the current blog. Do you think it's possible ? Do you know how to modify the code ?

Thank you very much.

cirotix’s picture

You should use the views module for that.
http://drupal.org/project/views

hgreer’s picture

I'm finishing up a big project and this was the missing piece! It works great in conjunction with the Contemplate module. I put the code into the template for my user profile and it worked right away!

Thanks again.

dzynz’s picture

Any way to add Author's picture?

pizangdesain’s picture

where you place that code? i've try to place your code to block body and use input format PHP, but it's not work
i also try to place the code to page visibility but it's not work.
can you help me?
I use Drupal 5

jfmoore’s picture

@mmahoney

Your code didn't work for me under D6 and PHP 5. I get the error:

"Fatal error: Call to undefined function db_num_rows() in ...public_html/drupal/includes/common.inc(1685) : eval()'d code on line 15"

I would appreciate any ideas.

patchak’s picture

You can use views multiblock now to easily create blocks that would grab an argument and publish related content-
Patchak