Francesco
Francesco

Reputation: 397

Joomla module get current article title

I'm trying to build a module that should show the article title of the current article viewed.

I've started from this code in my default.php layout that shows the title of the page, but I need to edit it so that it shows the article title and not the page title.

$heading = $document->getTitle();

How should I edit it to get the article title instead of the page title?

Upvotes: 0

Views: 3575

Answers (2)

jonasfh
jonasfh

Reputation: 4509

If this is in the normal article view, the following should work:

$input = JFactory::getApplication()->input;
$id = $input->getInt('id', 0);
if(
  $id > 0 
  && $input->getString('option') == 'com_content' 
  && $input->getString('view') == 'article'
) {
    $c = JTable::getInstance('content'); 
    $c->load($id);
    echo $c->title;
}

Upvotes: 3

Francesco
Francesco

Reputation: 397

I've solved this way:

$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);

    $heading = $article->get("title");
}

Not sure it's the correct/best solution, but it seems work at the moment. Any suggestion, comment about this code or better implementation?

Upvotes: 0

Related Questions