Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:00:47+00:00 2026-05-28T06:00:47+00:00

I’ve been trying to figure this out and make it work, but as I’m

  • 0

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.

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.

template.php

<?php
function dad_prev_next($current_node = NULL, $op = 'p') {
    if ($op == 'p') {
        $sql_op = '<';
        $order = 'DESC';
    } elseif ($op == 'n') {
        $sql_op = '>';
        $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' => $current_node -> nid));
    foreach ($result as $data) {
    }
    if (isset($data)) {            


        if ($op == 'n')            
            return l("Next", "node/$data->nid", array('html' => TRUE));
        else if ($op == 'p')
            return l("Previous", "node/$data->nid", array('html' => TRUE));
    }
}
?>

node.tpl.php

<?php print dad_prev_next($node,'p') . " / " . dad_prev_next($node,'n'); ?>

Working example

This one shows two previous/next images as it was designed

template.php

function dad_prev_next($nid = NULL, $op = 'p', $start = 0) {
    if ($op == 'p') {
        $sql_op = '>';
        $order = 'ASC';
    }
    elseif ($op == 'n') {
        $sql_op = '<';
        $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->leftJoin('field_data_field_photo', 'i', 'i.entity_id = n.nid');
    // join file managed table where all data about managed files stored
    $query->leftJoin('file_managed', 'f', 'f.fid = i.field_photo_fid');
    $query
        // select nid and title from node
        ->fields('n', array('nid', 'title'))
        // select uri from file_managed (image path)
        ->fields('f', array('uri'))
        // select image alt and title
        ->fields('i', array('field_photo_alt', 'field_photo_title'))
        // where nid "greater than"/"lower than" our current node nid
        ->condition('n.nid', $nid, $sql_op)
        // where node type in array('your content types')
        ->condition('n.type', array('photos'), 'IN')
        // where node is published
        ->condition('n.status', 1)
        // where requested node has image to display (if you want thumbnail)
        ->condition('f.uri', '', '!=')
        // order by nid
        ->orderBy('n.nid', $order)
        // limit result to 1
        ->range($start, 1);     

    // make query
    $result = $query->execute()->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' => 'square_small',
            'path' => $node->uri,
            'alt' => $node->field_photo_alt,
            'title' => $node->field_photo_title
        );
        $image = theme('image_style', $variables);

        $options = array(
            'html' => TRUE,
            'attributes' => array(
                'title' => $node->title
            )
        );
        $output = l($image, "node/{$node->nid}", $options);
    }

    return $output;
}

node.tpl.php

<?php print dad_prev_next($node->nid, 'p', 1); ?>
<?php print dad_prev_next($node->nid, 'p', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 1); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:00:48+00:00Added an answer on May 28, 2026 at 6:00 am

    template.php

    function dad_prev_next($nid = NULL, $op = 'p', $qty = 1) {
        if ($op == 'p') {
            $sql_op = '<';
            $order = 'DESC';
        }
        elseif ($op == 'n') {
            $sql_op = '>';
            $order = 'ASC';
        }
        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->leftJoin('field_data_field_IMAGEFIELD', 'i', 'i.entity_id = n.nid');
        // join file managed table where all data about managed files stored
        $query->leftJoin('file_managed', 'f', 'f.fid = i.field_IMAGEFIELD_fid');
        $query
            // select nid and title from node
            ->fields('n', array('nid', 'title'))
            // select uri from file_managed (image path)
            ->fields('f', array('uri'))
            // select image alt and title
            ->fields('i', array('field_IMAGEFIELD_alt', 'field_IMAGEFIELD_title'))
            // where nid "greater than"/"lower than" our current node nid
            ->condition('n.nid', $nid, $sql_op)
            // where node type in array('your content types')
            ->condition('n.type', array('photos'), 'IN')
            // where node is published
            ->condition('n.status', 1)
            // where requested node has image to display (if you want thumbnail)
            ->condition('f.uri', '', '!=')
            // order by nid
            ->orderBy('n.nid', $order)
            // limit result to $qty
            ->range(0, $qty);
    
        // make query
        $result = $query->execute()->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' => 'thumbnail',
                'path' => $node->uri,
                'alt' => $node->field_IMAGEFIELD_alt,
                'title' => $node->field_IMAGEFIELD_title
            );
            $image = theme('image_style', $variables);
    
            $options = array(
                'html' => TRUE,
                'attributes' => array(
                    'title' => $node->title
                )
            );
            $output = l($image, "node/{$node->nid}", $options);
        }
    
        return $output;
    }
    

    node.tpl.php

    <?php print dad_prev_next($node->nid, 'p', 2); ?>
    <?php print dad_prev_next($node->nid, 'n', 2); ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.