Lab / Drupal

Drupal7: Next & Prew node

Long time i had the idea to create Next & Prew links in node pages. For example to process a portfolio, or a section of the article. So today I made it.

First, you need to add this code on template.php

 function next_page_link($node){ $next_nid = db_query("
 SELECT nid FROM {node}
 WHERE created > :created AND type = :type AND status = 1
 LIMIT 1", 
array(':created' => $node->created,':type' => $node->type)) ->fetchObject(); if($next_nid) { return '/'.drupal_get_path_alias('node/'.$next_nid->nid); } else { return false; } } function previous_page_link($node){ $next_nid = db_query("
 SELECT nid FROM {node}
 WHERE created < :created AND type = :type AND status = 1
 ORDER BY created DESC
 LIMIT 1", 
array(':created' => $node->created,':type' => $node->type)) ->fetchObject(); 
if($next_nid) 
{
return '/'.drupal_get_path_alias('node/'.$next_nid->nid);
} else { 
 return false;
}
}

Nice, now last step, add this code on node.tpl.php

<?php $next = next_page_link($node); 
 $previous = previous_page_link($node); ?> 
 <?php if($previous !== false): ?> 
 <a href="<?=$previous; ?>" class="previouslink">Prew</a> 
 <?php endif; ?> <?php if($next !== false): ?> 
 <a href="<?=$next; ?>" class="nextlink">Next</a> 
<?php endif; ?>

So, it's all. Now you can use css for your individual design. Enjoy!