enter image description hereI have a content type called flower and I have created a node--flower.tpl.php for displaying products.

I am also using category of flowers. I need to add next and previous links on this page for a specific category. For example if I'm on rose page then all roses are needed to show on next and previous link, not other category data.

  • How do you want to decide which node is "Next" and which is "Previous"? For example it could maybe be by modified date, node ID, menu order, etc.
    – rooby
    Commented May 20, 2015 at 1:13
  • modified date.. but all node should same category..
    – jsh
    Commented May 20, 2015 at 7:37

2 Answers 2


You will have to write this logic yourself in a hook_preprocess_node(). This code should be added to the template.php file of your front end theme.

Here's an example:

function YOUR_THEME_preprocess_node(&$variables) {
  // Perform the logic for selecting prev/next terms here. Install and use
  // the Devel module to see what is available to you and to debug the code.

  // See what's available:

  // Load a sample term. Note: this should be different if your website is
  // in multiple languages. Below is just an example.
  $term = taxonomy_term_load($variables['node']->field_term[LANGUAGE_NONE][0]['tid']);

  // Pass a variable to the node--flower.tpl.php file. Code below will make
  // the $previous_category and $next_category variables available in the
  // node--flower.tpl.php file.
  $variables['previous_category'] = 'Your link here';
  $variables['next_category'] = 'Your link here';
  • I need to next and previous functionality for node not a term . can is it possible?
    – jsh
    Commented May 19, 2015 at 16:53
  • Yep. Use the above code and add your logic. If you explain what exactly do you mean by "next node" I might be able to help as well :) Commented May 19, 2015 at 16:58
  • I have attached image above please check.
    – jsh
    Commented May 19, 2015 at 17:29
  • Please let me know if it is possible.
    – jsh
    Commented May 20, 2015 at 4:48

Are you trying to get the previous and next node with a specific category? I'm not pretty sure but looks like in this case you have to do something like this in your node template

(i haven't tasted it)

$prev_nid = db_query("SELECT nid FROM {term_node} WHERE nid < :nid AND tid = TID ORDER BY nid DESC LIMIT 1", array(':nid' => $node->nid, ':tid' => $node->TAXONOMY_FIELD['und'][0]['tid']))->fetchField();

$next_nid = db_query("SELECT nid FROM {term_node} WHERE nid > :nid AND tid = TID ORDER BY nid LIMIT 1", array(':nid' => $node->nid, ':tid' => $node->TAXONOMY_FIELD['und'][0]['tid']))->fetchField();

if (@$prev_nid > 0) {@$prev_link = l('Previous', "node/$prev_nid");}
if (@$next_nid > 0) {@$next_link = l('Next', "node/$next_nid");}

print @$prev_link . ' - ' .@$next_link;
  • you have a couple of typos - :tid isn't in the query but is defined in the list of placeholders, and what would it taste like if you had? :)
    – Geoff
    Commented May 21, 2015 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.