0

I am using a key to connect with it's node nid but when I click on Next it gives path /node/node/256 instead of node/256. Here is my code:

$key = array_search($nid , $nids);
print $key;
$next = ($key + '1');
$previous = ($key - 1);
$total=count($nids);
$next_link =    "<a href='node/$nids[$next]'>Next</a>";
$previous_link = "<a href='node/$nids[$previous]'>Previous</a>";

$nids is an array. How to handle this situation?

2

Use l function to construct URLs in Drupal

$next_link =    l('Next', 'node/' . $nids[$next]);
$previous_link = l('Previous', 'node/' . $nids[$previous]);
  • thanx anil they show in url their nid if we want to show its alias then what i do? – Adi Mar 19 '14 at 12:24
  • 1
    @user25762 If a path has an alias, l() will use the alias automatically – Clive Mar 19 '14 at 13:38
0

Alternatively if you wanted just the alias, but not the whole link then you could use drupal_get_path_alias:

$alias = drupal_get_path_alias('node/'.$nid);
0

Correct next and prev node url by content type

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load($nid);
  $type = $node->type;

  $prev = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('n.status', 1, '=')
    ->condition('n.type', $type, '=')
    ->condition('n.created', $node->created, '<')
    ->orderBy('n.created', 'DESC')
    ->range(0, 1)
    ->execute()
    ->fetchField();

  $next = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('n.status', 1, '=')
    ->condition('n.type', $type, '=')
    ->condition('n.created', $node->created, '>')
    ->orderBy('n.created', 'ASC')
    ->range(0, 1)
    ->execute()
    ->fetchField();

  if ($prev) {
    $prev_link = l('Previous', 'node/' . $prev);

  }
  if ($next) {
    $next_link = l('Next', 'node/' . $next);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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