The Principles of Successful Freelancing »

Get Article ID in Joomla

Using PHP to get the article id of the article that your visitor is viewing is one of the more important pieces of information that you may need in writing custom themes.

You can use the following PHP code to get the article id...

<?php echo JRequest::getVar('id'); ?>

For the purpose of this example, this code displays out the article id to the page such as 82. But you can modify it to save the article id to a variable and use it to query the database and/or retrieve other pieces of information related to your article.

The code works if you put it in your template file such as in templates/yourtemplate/index.php

Be careful when SEF turned on

In some cases (especially when you have "search engine-friendly" SEF URLs tuned on, JRequest::getVar('id') may return something like "82:about-online-services"

In this case, you have to extract out the id from the returned string using PHP code similar to ...



$pulledId = JRequest::getVar('id');
if ( strpos($pulledId,':') > 0 ) {
$pulledId = substr($pulledId,0,strpos($pulledId,':'));
}

Now $pulledId should be the number before the colon (as in 82).