从模块显示的位置访问Joomla 3.2文章标题

6

我正在编写一个Joomla!模块,其中我需要显示当前文章的标题。

我在stackoverflow上找到了下面这段代码:

<?php
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
?>

虽然它可以工作,但是它使用了已经弃用的JRequest类,因为它来自Joomla 1.7,而我使用的是3.2.2。有人能告诉我如何重写它,以使其在Joomla 3.2中有效吗?

1个回答

15

您可以使用以下符合最新编码标准的代码:

$input = JFactory::getApplication()->input;
$id = $input->getInt('id'); //get the article ID
$article = JTable::getInstance('content');
$article->load($id);

echo $article->get('title'); // display the article title

希望这可以帮到你


我不敢忘记 :) 只是需要等待几分钟,直到stackoverflow允许我。 - zorza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接