Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I only needed next | prev so I simplified the code a bit and added append prepend text support. This goes to template.php. I also had do fix the first SQL query.
function next_prev($current_nid, $type, $button_type, $label, $prepend_text=NULL, $append_text=NULL) {
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} tn INNER JOIN {node} n ON tn.nid = n.nid WHERE tn.nid = $current_nid")));
switch ($button_type) {
case 'next':
$sort= 'DESC';
$case = '< ';
break;
case 'prev':
$sort = 'ASC';
$case = '> ';
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 $prepend_text.l($label, 'node/'. $result['nid'], array('title' => $result['title'])).$append_text;
}
}
In node.tpl.php I put this
if($page!=0&& $node->type=='image')
{
$previous_node_link = next_prev($node->nid, $node->type, 'prev', t('previous'), '<< ', NULL);
$next_node_link = next_prev($node->nid, $node->type, 'next', t('next'), NULL, ' >>');
print '<div class="previous-next-links">';
if($previous_node_link && $next_node_link)
{
print $previous_node_link.' | '.$next_node_link;
}
else if($previous_node_link)
{
print $previous_node_link;
}
else if($next_node_link)
{
print $next_node_link;
}
print '</div>';
}
Comments
Is possible to combine this code with DraggableViews module?
I’m using DraggableViews module.
I create a view where I show all thumbnails of my gallery. Using DraggableViews my client can sort the pictures as he wants giving a specific order to the thumbnails.
When somebody makes click in a thumbnail, it calls the node related with it.
So… There is a way to call the “Next” and “Previews” node following the value or order the nodes have on the gallery I created with the DraggableViews?
Thanks,
Ana.
I want to create next|prev for image gallery
I followed all steps.still i am no getting.what is wrong there?
function next_prev($current_nid, $type, $button_type, $label, $prepend_text=NULL, $append_text=NULL) {
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} tn INNER JOIN {node} n ON tn.nid = n.nid WHERE tn.nid = $current_nid")));
switch ($button_type) {
case 'next':
$sort= 'DESC';
$case = '< ';
break;
case 'prev':
$sort = 'ASC';
$case = '> ';
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 $prepend_text.l($label, 'node/'. $result['nid'], array('title' => $result['title'])).$append_text;
}
}