Retrieve title/status with just a NID

Public

You sometimes need to output a link to a node, with a static nid but the title can be changed and you want it to be reflected in your page (example : a "legal info" link out a footer menu).

This function allow you to easily retrieve a title with just a nid.

</> CopyGet raw version
php
  1. function get_title( $nid ) {
  2. return db_query("SELECT title FROM {node} WHERE nid = :nid LIMIT 1", array(":nid": => $nid ) )->fetchField();
  3. }
  4.  
  5. print l( get_title( $nid ), "node/" . $nid );

D6

</> CopyGet raw version
php
  1. function get_title( $nid ) {
  2. return db_result( db_query("SELECT title FROM {node} WHERE nid = %d LIMIT 1", $nid));
  3. }
  4.  
  5. print l( get_title( $nid ), "node/" . $nid );

You might to check if this page is not unpublished before rendering this link :

</> CopyGet raw version
php
  1. function get_status( $nid ) {
  2. return db_result( db_query("SELECT status FROM {node} WHERE nid = %d LIMIT 1", $nid));
  3. }
  4.  
  5. print (get_status( $nid ) ? l( get_title( $nid ), "node/" . $nid ) : "");

D6

</> CopyGet raw version
php
  1. function get_status( $nid ) {
  2. return db_result( db_query("SELECT status FROM {node} WHERE nid = %d LIMIT 1", $nid) );
  3. }
  4.  
  5. print (get_status( $nid ) ? l( get_title( $nid ), "node/" . $nid ) : "");