Project:Content Templates (Contemplate)
Version:6.x-1.2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

I'm trying to link NEWs content type's teasers to their nodes using the $node->path variable, and it works just fine from the front page, but when outputting a teaser from an inner page the url its wrongly constructed, since it adds the node path to the current URL. ¿How could I fix this?

best regards,

Bahr

Comments

#1

An example node path would be 'node/123' or 'path/to/node'. The reason you are having problems is the way HTML works to interpret where you want the user to be directed.

There are three ways a url can be written.

  1. <a href="node/123">

    This method is relative. The browser will take you to a page relative to the current page. If you are at http://mysite.com/test/421 this link will take you to http://mysite.com/test/node/123

  2. <a href="/node/123">

    This method is relative to the root of the site. If you are at http://mysite.com/test/421 this link will take you to http://mysite.com/node/123

  3. <a href="http://www.example.com/node/123">

    This method is abcolute. If you are at http://mysite.com/test/421 this link will take you to http://www.example.com/node/123

If you use <a href="<?php print $node->path; ?>"> you get #1, a relative link. You could use <a href="/<?php print $node->path; ?>"> to make it a link relative to the site root, but what is your site is in a sub directory like http://mysite.com/drupal, you would have to use <a href="/drupal/<?php print $node->path; ?>">.

To make it easier Drupal provides a few functions that will help with your coding.

You can use url(). Url() automatically adds the Drupal $base_path to the url.
Examples"

<?php

// returns a url relative to the site root: '/drupal/node/123'
print url('node/123');

// returns an absolute url: 'http://www.mysite.com/node/123'
print url('node/123', array('absolute' => TRUE));
?>

You can user l(). l() automatically adds the Drupal base path and returns a themed link.

Examples:

<?php

// returns a url relative to the site root: <a href="/drupal/node/123">My Link</a>
print l('Mu Link', 'node/123');

// returns an absolute url: <a href="http://www.mysite.com/node/123">My Link</a>
print l('My Link', 'node/123', array('absolute' => TRUE));
?>