View by same author

mmahoney - January 13, 2007 - 13:46

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

Did you look through the

pwolanin - January 13, 2007 - 15:52

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() )

Pucker up

mmahoney - January 14, 2007 - 23:50

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;
}
?>

This also works in Drupal

jaks1970 - January 27, 2007 - 08:55

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.

Well, I'm not an expert in this, but

mmahoney - February 25, 2007 - 14:11

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.

Another question here, How

patchak - February 25, 2007 - 14:26

Another question here,

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

Doesn't work with dbase prefix

Andrzej7 - February 26, 2007 - 23:32

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

I fixed it manually.

But still a great work :-)

How to modify the code for user's blog ?

bumathan - March 17, 2007 - 20:01

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.

Use views

cirotix - June 28, 2007 - 11:17

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

Thank you!

hgreer - August 29, 2007 - 01:20

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.

Any way to add Author's

dzynz - October 17, 2007 - 03:04

Any way to add Author's picture?

where you place that code?

pizangdesain - May 16, 2009 - 21:19

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

@mmahoney Your code didn't

jfmoore - November 12, 2009 - 20:50

@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.

You can use views multiblock

patchak - October 26, 2007 - 08:39

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

 
 

Drupal is a registered trademark of Dries Buytaert.