0

I have created a custom node, and at the bottom of this node I would like to show the latest three comments made on that node. I have added the code below to my template.php, and then loop through the array to get the data in my node:

if ((arg(0) == 'node') && is_numeric(arg(1))) 
{
    $result = db_query_range('SELECT c.cid, c.nid, c.subject, c.name
    FROM {comment} c WHERE c.nid = :cid ORDER BY c.cid DESC', 0, 3, array(':cid' => arg(1)));

    $i = 0;     
    foreach ($result as $record) {
        $variables['NewComments']['NewSubject'][$i] = $record -> subject;
        $variables['NewComments']['NewName'][$i] = $record -> name;
        $i++;
    }
}

else {
    $variables['errorCheck'] = t('nope fail');
}

However, I am having trouble outputting this into the node.tpl. Is there an easy way to do this, or should I try something else to tackle this issue?

Thanks

1 Answer 1

1

You can do that using views.

enter image description here

enter image description here

enter image description here

Then you can either add this view as a block or you can use http://api.drupal.org/api/views/views.module/function/views_embed_view/7 in node.tpl.php

1
  • Thanks, this worked very well. I'm going from D6 to D7 and the views module seems to have changed... Is there a way to make it show just comments subjects without them becoming links?
    – RedRaven
    Dec 24, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.