Skip to content

Instantly share code, notes, and snippets.

@mynameispj
Last active October 7, 2015 02:27
Show Gist options
  • Save mynameispj/3090398 to your computer and use it in GitHub Desktop.
Save mynameispj/3090398 to your computer and use it in GitHub Desktop.
Next / previous node links in Drupal 7
switch($n_node->type) {
case 'your-custom-content-type-machine-name-here':
$html = l('"'.$n_node->title .'"', 'node/'.$n_node->nid, array('html' => TRUE, 'attributes' => array('class' => array('prev-next'), 'title' => $n_node->title)));
return $html;
}
switch($n_node->type) {
case 'your-custom-content-type-machine-name-here':
$html = l('"'.$n_node->title .'"', 'node/'.$n_node->nid);
return $html;
}
<?php
$nextPost = pn_node($node, 'n');
$prevPost = pn_node($node, 'p');
if ($nextPost != NULL) { ?>
<?php print $nextPost; ?>
<?php } ?>
<?php if ($prevPost != NULL) { ?>
<?php print $prevPost; ?>
<?php } ?>
<ul>
<li><?php print pn_node($node, 'n'); ?></li>
<li><?php print pn_node($node, 'p'); ?></li>
</ul>
function pn_node($node, $mode = 'n') {
if (!function_exists('prev_next_nid')) {
return NULL;
}
switch($mode) {
case 'p':
$n_nid = prev_next_nid($node->nid, 'prev');
$link_text = 'previous';
break;
case 'n':
$n_nid = prev_next_nid($node->nid, 'next');
$link_text = 'next';
break;
default:
return NULL;
}
if ($n_nid) {
$n_node = node_load($n_nid);
switch($n_node->type) {
case 'article':
$options = array('absolute' => TRUE);
$nid = $n_node->nid;
$url = url('node/' . $nid, $options);
$html = '<a class="button" href="'. $url .'" />' . $n_node->title . '</a>';
return $html;
}
}
}
function pn_node($node, $mode = 'n') {
if (!function_exists('prev_next_nid')) {
return NULL;
}
switch($mode) {
case 'p':
$n_nid = prev_next_nid($node->nid, 'prev');
$link_text = 'previous';
break;
case 'n':
$n_nid = prev_next_nid($node->nid, 'next');
$link_text = 'next';
break;
default:
return NULL;
}
if ($n_nid) {
$n_node = node_load($n_nid);
switch($n_node->type) {
case 'article':
$html = l('"'.$n_node->title .'"', 'node/'.$n_node->nid);
return $html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment