How To Get A Joomla 2.5 Article Title In Template

Today I had to get the title for an article in Joomla 2.5 for my template, I was having to display the title somewhere else, so here is some php code that will do just that!

// get option and view
$option = JRequest::getVar('option');
$view = JRequest::getVar('view');

// make sure it is a single article
if ($option == 'com_content' && $view == 'article'):
  $id = JRequest::getInt('id');
  $article =& JTable::getInstance('content');
  $article->load($id);
  $article_title = $article->get('title');
endif;

This will work anywhere you need it to. I just needed it for my template, but it could work in a module, plugin or component.

EXTRA: And this can be used for getting any type of information from the article, not just the title. You can use this and get any of the fields linked with the article, just replace the get(‘title’); with the field name you need, as follows:

// get other article fields
$article_alias = $article->get('alias');
$article_introtext = $article->get('introtext');
$article_fulltext = $article->get('fulltext');