Drupal: Adding a “More from this author” block using CCK author field

Problem:

Show a “More From this author” Block on nodes of specific types without using the $uid variable provided by Drupal.

Let’s assume that you don’t want to use the “Posted by: username” on your stories/nodes, but rather you have your own CCK field setup to display the name of the author.  Now this CCK field may be a reference field to author pages, or just simple text fields where the authors are typing their names.  To show a block in your sidebar that displays more articles from the same author, you will want to use Views.

Solution:

This applies for Drupal 6.x only:

  • Add a new view
  • Add your fields (e.g. node->title)
  • Add your filters (e.g. node->published->yes; node->types->select your types)
  • Add your sort criteria (e.g. node->published date->desc)

Now it’s time to add the argument.  Assuming the machine-readable name of your “Author name” field is “field_byline”, you will have to add a new argument.  Select your field from the Content section and change the “Action to take if argument is not present” to “Provide default argument”.  Now in the following section, select “PHP code” and enter the following code:

if (arg(0) == ‘node’ && is_numeric(arg(1))) {
$node=node_load(arg(1));
$by_line = $node->field_byline[0][‘value’];
}
return $by_line;

Now, go ahead and add a “Block display” to the view and place the block on any content type.  You should see the recent posts, based on the number of items you selected in your view, in a block.

Refining this view further, we should be excluding the current node so that it doesn’t show up in the list of nodes in the block.  To do this, simply add another argument, select Node: NodeID from the Node group.  Provide the default argument and select Node ID from URL as the option.  At the bottom, select “Exclude the argument”.  Save your view and visit your node again.  You will find that the node you are currently visiting will not show up in the list.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">