Joomla plugin : how to get article title and article id

断了今生、忘了曾经 提交于 2019-11-30 15:20:35
h_45h

i tried :

public function onContentPrepare($context,&$article, &$params, $limitstart) {
 echo JRequest::getVar('id');
}

Still I am not getting the id. Is this right?

The article is loaded in your second argument ($article). Being on this event (onContentPrepare), the only property you can access is $article->text.

For suiting your purpose (getting the article id and title) you will want to use another event, called "onContentBeforeDisplay".

public function onContentBeforeDisplay($context, &$article, &$params, $limitstart)

Here you have (again) the article passed through the second argument, but now you have access to properties like $article->id, $article->title and many others.

For future references on content events, take a look at the file "plugins\content\example\example.php"

In order to get the article ID you have to write the following:

echo JRequest::getVar('id');

For the title, you just take the id you got, load the article object

$blabla = $article->load(ID);
echo $blabla->get('title');

It seems JRequest is deprecated in 2.5 & 3.x as indicated in the Deprecated Elements list.

I would rather use the following:

$article_id = JFactory::getApplication()->input->get('id');

You can use for getting active article title like this

$menu =& Jsite::getMenu(); echo $menu->getActive()->title;

may this help.

To get Joomla article id use this...

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

In the previous answer someone used id instead of Itemid. Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!