I'm using the following code to print a list of links to the 5 latests nodes published. The code is used outside Drupal's directory:

At the very bottom:

<?php
$drupal_dir = "/home/username/public_html/blog";
$current_dir = getcwd();
chdir($drupal_dir);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); ?>

And then, where I want to print the list

<?php
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = 'story' AND node.status = 1 ORDER BY node.created DESC LIMIT 5";
$output .= "<ul class='lista'>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print $output;
chdir($current_dir); ?>

The problem is that the links are not published correctly. Drupal's location is at blog.domain.com and I'm using the snippet at www.domain.com, so all the links are published like www.domain.com/blog-url. Does anyone know how to fix this so the links are published correctly?

Comments

Mark Theunissen’s picture

Yep, you could change the following line:


$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";

to this:


$output .= "<li>".l($anode->title, "http://blog.domain.com/node/$anode->nid")."</li>";

That will hard-code the address of blog.domain.com into the link.

Cheers
Mark

Code Baboon
Drupal based services

Duplika’s picture

Thanks for your quick reply Mark. I've tried that but it shows "node/13" instead of "node/url-alias-generated-by-pathauto". It is what I'm using now but I'd love to see if there's a way to link to the "aliased" node.
--
Duplika | Web Hosting

vm’s picture

I think it's using the nid because that is what you are calling with ->nid

I think you have to pass the nid into http://api.drupal.org/api/function/drupal_get_path_alias/5

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

Mark Theunissen’s picture

That's correct... change the code to this then:


$output .= "<li>".l($anode->title, "http://blog.domain.com/" . drupal_get_path_alias("node/$anode->nid"))."</li>";

Mark

Code Baboon
Drupal based services

Duplika’s picture

Awesome! It's working perfectly well now, thanks for your time guys.

mooffie’s picture

Drupal's location is at blog.domain.com and I'm using the snippet at www.domain.com, so all the links are published like www.domain.com/blog-url. Does anyone know how to fix this

Do $base_url = 'http://blog.domain.com'; before you bootstrap Drupal.

Duplika’s picture

I'm having problems with this again. It seems to work for quite a while but later it just replaces the whole page with the homepage of the blog. I have no clue if this has to do with some sort of cache at server level but I need to find another way to do this.

Does anyone know how? Maybe just doing a simple a quick mysql query, without bootstrapping Drupal? Any help will be appreciated.
--
Duplika | Web Hosting

vm’s picture

I believe you must bootstrap drupal or you can't get into the DB Drupal uses as I believe that would constitue a security hole.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

Duplika’s picture

I've disabled Drupal's cache and everything seems to be working, at least for now.

Do you have any clue what could be happening?
--
Duplika | Web Hosting

vm’s picture

not off the top of my head no.

what was caching set @ ? normal ? aggressive ?

in reference to chaching you are talking about Drupal internal caching yes? in administer -> performance ?

are you using any of the other caching modules ?

have you looked at the cache table in the DB to see if there are any anomalies ?

have you tried clearing your cache table in the DB ? si that the cache will all be regenerated ?

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

Duplika’s picture

Thanks for your reply and for taking some time to check this.

The cache was set to normal and yeah, I'm taking about Drupal's internal cache system found at administer -> performance. I'm not using any other cache modules except the ones at server level like eaccelerator.

I haven't found any anomalies at the cache table, though I think I will keep it this way (with cache disabled) as long as it works.
--
Duplika | Web Hosting