title: Prev/Next node navigation with a thumbnail in a full node
body: <p>I've been trying to figure this out and make it work, but as I'm not a programmer, just a designer with limited coding knowledge I've been hitting a wall on this one.</p>
  
  <p>I'm making my own photo gallery and I would like to display two previous or next images as linked thumbnails. I've found this script that works and shows textual links, but don't know how to adapt it to my needs.</p>
  
  <p><strong>template.php</strong></p>
  
  <pre><code>&lt;?php
  function dad_prev_next($current_node = NULL, $op = 'p') {
      if ($op == 'p') {
          $sql_op = '&lt;';
          $order = 'DESC';
      } elseif ($op == 'n') {
          $sql_op = '&gt;';
          $order = 'ASC';
      } else {
          return NULL;
      }
  
      $output = NULL;
  
      $sql = "SELECT n.nid, n.title
               FROM {node} n
               WHERE n.nid $sql_op :nid
               AND type IN ('photos')
               AND status = 1
               ORDER BY nid $order
               LIMIT 1";
      $result = db_query($sql, array(':nid' =&gt; $current_node -&gt; nid));
      foreach ($result as $data) {
      }
      if (isset($data)) {            
  
  
          if ($op == 'n')            
              return l("Next", "node/$data-&gt;nid", array('html' =&gt; TRUE));
          else if ($op == 'p')
              return l("Previous", "node/$data-&gt;nid", array('html' =&gt; TRUE));
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>node.tpl.php</strong></p>
  
  <pre><code>&lt;?php print dad_prev_next($node,'p') . " / " . dad_prev_next($node,'n'); ?&gt;
  </code></pre>
  
  <h1>Working example</h1>
  
  <p>This one shows two previous/next images as it was designed</p>
  
  <p><strong>template.php</strong></p>
  
  <pre><code>function dad_prev_next($nid = NULL, $op = 'p', $start = 0) {
      if ($op == 'p') {
          $sql_op = '&gt;';
          $order = 'ASC';
      }
      elseif ($op == 'n') {
          $sql_op = '&lt;';
          $order = 'DESC';
      }
      else {
          return NULL;
      }
  
      $output = '';
  
      // your node must have an image type field
      // let's say it's name is IMAGEFIELD
      // select from node table
      $query = db_select('node', 'n');
      // join node table with image field table
      $query-&gt;leftJoin('field_data_field_photo', 'i', 'i.entity_id = n.nid');
      // join file managed table where all data about managed files stored
      $query-&gt;leftJoin('file_managed', 'f', 'f.fid = i.field_photo_fid');
      $query
          // select nid and title from node
          -&gt;fields('n', array('nid', 'title'))
          // select uri from file_managed (image path)
          -&gt;fields('f', array('uri'))
          // select image alt and title
          -&gt;fields('i', array('field_photo_alt', 'field_photo_title'))
          // where nid "greater than"/"lower than" our current node nid
          -&gt;condition('n.nid', $nid, $sql_op)
          // where node type in array('your content types')
          -&gt;condition('n.type', array('photos'), 'IN')
          // where node is published
          -&gt;condition('n.status', 1)
          // where requested node has image to display (if you want thumbnail)
          -&gt;condition('f.uri', '', '!=')
          // order by nid
          -&gt;orderBy('n.nid', $order)
          // limit result to 1
          -&gt;range($start, 1);     
  
      // make query
      $result = $query-&gt;execute()-&gt;fetchAll();
  
      foreach ($result as $node) {
          // theme your thumbnail image
          $variables = array(
              // default image style name `thumbnail`
              // you can use your own by following
              // admin/config/media/image-styles on your site
              'style_name' =&gt; 'square_small',
              'path' =&gt; $node-&gt;uri,
              'alt' =&gt; $node-&gt;field_photo_alt,
              'title' =&gt; $node-&gt;field_photo_title
          );
          $image = theme('image_style', $variables);
  
          $options = array(
              'html' =&gt; TRUE,
              'attributes' =&gt; array(
                  'title' =&gt; $node-&gt;title
              )
          );
          $output = l($image, "node/{$node-&gt;nid}", $options);
      }
  
      return $output;
  }
  </code></pre>
  
  <p><strong>node.tpl.php</strong></p>
  
  <pre><code>&lt;?php print dad_prev_next($node-&gt;nid, 'p', 1); ?&gt;
  &lt;?php print dad_prev_next($node-&gt;nid, 'p', 0); ?&gt;
  &lt;?php print dad_prev_next($node-&gt;nid, 'n', 0); ?&gt;
  &lt;?php print dad_prev_next($node-&gt;nid, 'n', 1); ?&gt;
  </code></pre>