I would like to add previous and next buttons to each node of my Drupal website.
These links are supposed to point to the next node in the website content.
How can I make it ? Thanks
Here's a D7 version of aterchin's code.
<?php
/**
* Previous / Next function for nodes, ordered by node creation date
*
* @param $current_node: node object or node id
* @param $node_types: array of node types to query
*
* @return array
*
*/
function MODULE_prev_next_node($current_node = NULL, $node_types = array()) {
// make node object if only node id given
if (!is_object($current_node)) { $current_node = node_load($current_node->nid); }
// make an array if string value was given
if (!is_array($node_types)) { $node_types = array($node_types); }
// previous
$prev = db_select('node', 'n')
->fields('n',array('nid','title','created'))
->condition('n.status', 1,'=')
->condition('n.type', $node_types,'IN')
->condition('n.created', $current_node->created,'<')
->orderBy('created','DESC')
->range(0,1)
->execute()
->fetchAssoc();
// next or false if none
$next = db_select('node', 'n')
->fields('n',array('nid','title','created'))
->condition('n.status', 1,'=')
->condition('n.type', $node_types,'IN')
->condition('n.created', $current_node->created,'>')
->orderBy('created','ASC')
->range(0,1)
->execute()
->fetchAssoc();
return array('prev' => $prev, 'next' => $next);
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With