A better way to do this using Custom Pagers module is detailed in the Image module documentation: http://drupal.org/node/709462
description
The following adds a custom next_prev
function to your theme template.php file that allows you to insert NEXT | PREVIOUS type links in your layout.tpl.php files. Where layout might be node.tpl.php, node-image.tpl.php etc.
In the illustrative example snippet below, a simple NEXT | PREVIOUS link is inserted into a custom node-image.tpl.php file linking to the next and previous image within that gallery (taxonomy term).
step 1 of 2
Add the custom next_prev
function to your template.php file.
- In a text editor like notepad.exe or equivalent, create/edit a template.php file and paste the following snippet
- Save your new/edited template.php file and upload it to your active theme folder
function next_prev for Drupal 5
<?php
function next_prev($current_nid, $type, $button_type, $label, $class) {
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} WHERE nid = $current_nid;")));
if (empty($tid)){ //validate that the image 'does' have a tid
return '';
}
switch ($button_type) {
case 'next':
$sort= 'ASC';
$case = '> ';
break;
case 'prev':
$sort = 'DESC';
$case = '< ';
break;
case 'parent':
$name = db_result(db_query(db_rewrite_sql("SELECT name FROM {term_data} WHERE tid = $tid;")));
return l($label.$name, "$type/tid/$tid", array('title' => $name, 'class' => $class));
break;
case 'last':
$sql = "SELECT n.nid, n.title FROM node n INNER JOIN term_node t ON n.nid = t.nid";
$sql .= " INNER JOIN term_data r ON t.tid = r.tid WHERE n.type = '". $type ."'";
$sql .= " AND r.tid = $tid AND n.status = 1 ORDER BY nid ASC LIMIT 0 , 1";
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
return l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
break;
case 'first':
$sql = "SELECT n.nid, n.title FROM node n INNER JOIN term_node t ON n.nid = t.nid";
$sql .= " INNER JOIN term_data r ON t.tid = r.tid WHERE n.type = '" .$type. "'";
$sql .= " AND r.tid = $tid AND n.status = 1 ORDER BY nid DESC LIMIT 0 , 1";
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
return l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
break;
default:
return NULL;
break;
}
$sql = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid ";
$sql .= "INNER JOIN {term_data} r ON t.tid = r.tid WHERE n.type = '". $type ."' AND n.nid ". $case;
$sql .= $current_nid ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY nid ". $sort;
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
if (!$result) {
return NULL;
} else {
return l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
}
}
?>
function next_prev for Drupal 6
<?php
function next_prev($current_nid, $type, $button_type, $label, $class) {
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} WHERE nid = %d"), $current_nid));
if (empty($tid)){ //validate that the node 'does' have a tid
return;
}
switch ($button_type) {
case 'next':
$sort= 'ASC';
$case = '> ';
break;
case 'prev':
$sort = 'DESC';
$case = '< ';
break;
case 'parent':
$name = db_result(db_query(db_rewrite_sql("SELECT name FROM {term_data} WHERE tid = %d"), $tid));
return l($label.$name, "taxonomy/term/$tid", array('title' => $name, 'class' => $class));
case 'last':
$sql = "SELECT n.nid, n.title FROM node n INNER JOIN term_node tn ON n.nid = tn.nid";
$sql .= " INNER JOIN term_data td ON tn.tid = td.tid WHERE n.type = '%s'";
$sql .= " AND td.tid = %d AND n.status = 1 ORDER BY nid ASC LIMIT 1";
$result = db_fetch_array(db_query(db_rewrite_sql($sql), $type, $tid));
return l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
case 'first':
$sql = "SELECT n.nid, n.title FROM node n INNER JOIN term_node tn ON n.nid = tn.nid";
$sql .= " INNER JOIN term_data td ON tn.tid = td.tid WHERE n.type = '%s'";
$sql .= " AND td.tid = %s AND n.status = 1 ORDER BY nid DESC LIMIT 1";
$result = db_fetch_array(db_query(db_rewrite_sql($sql), $type, $tid));
return l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
default:
return;
}
$sql = "SELECT n.nid, n.title FROM {node} n
INNER JOIN {term_node} tn ON n.nid = tn.nid
INNER JOIN {term_data} td ON tn.tid = td.tid
WHERE n.type = '%s' AND
n.nid " . $case . "%d AND
td.tid = %d AND
n.status = 1
ORDER BY nid ". $sort . " LIMIT 1";
$result = db_fetch_array(db_query(db_rewrite_sql($sql), $type, $current_nid, $tid));
if (!$result) {
return;
} else {
return l($label, 'node/'. $result['nid'], array('attributes'=>array('class'=>$class, 'title'=>$result['title']), 'html'=>TRUE));
}
}
?>
step 2 of 2
Insert this snippet in your custom layout.tpl.php file and edit the $node->type
to suit. This example snippet is intended for a custom node-image.tpl.php file. It inserts a link to the NEXT and PREVIOUS image within that image gallery (taxonomy term).
<?php
if ($terms && arg(0) == 'node' && is_null(arg(2))) {
$next = next_prev($node->nid, 'image', 'next', 'next>', 'link'); // replace 'test' with a valid class for the link
$previous = next_prev($node->nid, 'image', 'prev', '<previous', 'link');
$last = next_prev($node->nid, 'image', 'last', 'last>>', 'link');
$first = next_prev($node->nid, 'image', 'first', '<<first', 'link');
$gallery = next_prev($node->nid, 'image', 'parent', '', 'link');
print '<p><center>';
if ($previous){ print $first .' '. $previous;}
print ' ['. $gallery .'] ';
if ($next){ print $next .' ' .$last;}
print '</center></p>';
}
?>
notes
- once added to your template.php file, the
next_prev
function maybe called from any custom layout.tpl.php file. - Please add a comment/child page with examples of using the custom
next_prev
function in your layout.tpl.php files.
Comments
Small fix to step 1
This is a super-useful piece of code and was working fine for me until I installed I18n, at which point I started getting a bunch of SQL errors like the following:
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';)' at line 1 query: SELECT tid FROM term_node LEFT JOIN i18n_node i18n ON n.nid = i18n.nid WHERE (i18n.language ='en' OR i18n.language ='' OR i18n.language IS NULL) AND ( nid = 125;) in /home/digitao0/public_html/ecologic/includes/database.mysql.inc on line 172.
I fooled around with this a bit and discovered that some small changes to the SQL (adding brackets to all the tables that get referred to, removing the semicolon from the first SQL statement, adding a LEFT JOIN to bring the node table into that statement) fixed the problem. Here's what works for me:
a little correction more
with this code:
You lose the name of the parent gallery. The right code are the lines of the original version of the snippet. I solved it.
Bye! and thanks
Saigon... shit; I'm still only in Saigon.
A little correction
Hi,
I got an error on this snippet until I found the problem, with italian community help.
Drupal got an sql error because of the missing {} in the first two groups of sql queries.
You see, on the case 'default' the tables name are between {} and Drupal finds them.
My Drupal didn't find the tables in the case 'last' and 'first', I had to put the table names between {} and with this correction finally it's working.
Bye!
Saigon... shit; I'm still only in Saigon.
Sorting by Authored Date...
Thank you so much for these extremely helpful snippets!
I do have a question however... these snippets sort by node ID. I am entering a back catalog of content, so the IDs are out of order. Can someone assist me in changing the SQL lines so that the nodes returned are sorted by Authored Date?
Any help would be tremendously appreciated!
And i am looking for a
And i am looking for a solution for sorting it by a cck-field.
I have one "sort_order" to change the sort-order manual. And now i want to sort the prev/next by this field.
can anyone help me?
Or did you find a solution, camb?
Shall this work on Drupal 6?
I am extremely interested in this! Image gallery without "next" and "previous" in each photo is extremely tiresome and will blow away any visitor.
Perfect!
I was searching for a solution exactly like that for about 1 week. Thank you so much!
The "
" Phrases i was replacing with an and it works perfect. Now i need to replace the link to the gallery and i don't know how? When i replace the "'parent'" - there was nothing to show. No textlink and no image. When i replace the "$gallery" - there was showing the image but without a link around that. Can anyone help me to get the gallery-link replacing with an image? Thank you for reading that.I got it working with this
I got it working with this Code in node.tpl.php:
D7?
And in D7?
Drupal 7
Is there a version for drupal 7?
+1
+1
Drupal 7
Drypal 7 function
Using
I've got this working, but
(for Drupal 6) I've got this working, but It's not pulling from the correct taxonomy. Anyone know how to pick the taxonomy vocabulary it uses?
Thanks so much,
Evan
D7
D7 variant
template.php
node.tpl.php
Doesn't this overload the
Doesn't this overload the database? especially when you have over 1000 users viewing at the same time (caching only helps only a little)
Thank you all for this
Very helpful and exactly what I needed.
I needed this for a couple of projects with a few variations, so I turned this snippet into a little module. You can change the tpl and select whether the labels say "Next" and "Previous" or display the next and previous node titles. Please feel free to take it and put it to use!
-Floyd
D7 extend next/prev links within taxonomy terms per language
template.php
function to get current language:
the above function for next/prev links
node.tpl.php