I want to theme the Recent Content block with associated thumbnail of each node. After researching for a long time I found the function node_get_recent(). This function selects data from table 'node', but this table doesn't include any url pointing to the url of an image, which means I am not be able to use $node->(url of this thumbnail) to fetch its thumbnail. I happend to find the url of the thumbnail is located in the table 'file_managed', but I still cannot associated it with table 'node' through leftjoin.

I do not know how to do it the right way. I do not want to use Views. Thanks!

When I put the following code into my .module file the title displayed for each node is the same.

function example_contents() {
  $number_of_post = 10;
  $result = db_query("select n.title,n.nid,f.entity_id,fm.fid,fm.uri from {node} n left join {field_data_field_image} f on n.nid=f.entity_id left join {file_managed} fm on f.field_image_fid=fm.fid   limit $number_of_post");
  $items = array();
  foreach ($result as $node) {
    $items[] = l($node -> title,'node/'.$node -> nid);
  return array('#markup' => theme('item_list', array('items' => $items)));

$node -> title turned out to be same, however $node -> nid worked. That is to say the list of nodes used the same title but different urls. What was wrong with my code? Secondly, I do not know where to put my $node -> uri into $items[] in order to display the thumbnail.

  • 4
    Why don't you want to use Views? Generally speaking, this is exactly what Views was designed to do. Commented Apr 14, 2012 at 5:52
  • 1
    flexibility is limited as well as the effectivness of the site.
    – Drupal cai
    Commented Apr 14, 2012 at 5:54
  • What version of Drupal are you using?
    – sheena_d
    Commented Apr 18, 2012 at 8:45
  • OK,I know what was wrong,devel generated a chunk of articles began with the same few words which made me thought they were one article,everything works,thanks!
    – Drupal cai
    Commented Apr 20, 2012 at 13:40
  • Please include the drupal version you are working on. Looking at the question it is very difficult to figure out.
    – junedkazi
    Commented Sep 25, 2012 at 20:35

2 Answers 2


to slecet image uri you have to do

selest uri from node
join field_data_filed_image on field_data_filed_image.entity_id = node.nid
join file_managed on filed_image_fid = fid

anfd you can use this function to get thumbnail using uri

<img class="image" src="<?php print image_style_url('thumbnail', $block->uri) ?>"/> 
  • if you use node_load only you will need image_style_url('thumbnail',$node->uri);
    – Ahmed
    Commented Apr 14, 2012 at 7:51

Looking at the sql query I am assuming it is Drupal 7 so you can also use entity field query

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'node type')
      ->propertyCondition('status', 1);
$result = $query->execute();
$nodes = node_load_multiple(array_keys($result['node']));  

After you get a list of nodes you can do a foreach loop over all the nodes and you will have the image field available in each node object.

Also for more documentation about field entity query and options please refer to http://drupal.org/node/1343708 http://api.drupal.org/api/drupal/includes!entity.inc/class/EntityFieldQuery/7

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.