4

I have developed on simple plugin in Joomla 1.6 I stuck at : How to get article title and article url.

I am getting no output if tried to print below statement:

echo $article->title;
echo $article->id;

I have written this in php file, not used MVC architecture. Is there any other settings need to do in Joomla admin side ?

Please suggest your pointers for extracting article title and article url.

Thanks in advance!

Pravin

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
pravin
  • 2,155
  • 8
  • 37
  • 49
  • To check if the code is currently displaying an article, make sure `JRequest::getCmd('view', '')` returns `"article"`. – Flimm Apr 21 '16 at 09:55

5 Answers5

14

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');
Asaf
  • 8,106
  • 19
  • 66
  • 116
  • i tried : public function onContentPrepare($context,&$article, &$params, $limitstart) { echo JRequest::getVar('id'); } Still I am not getting id...is this write ? – pravin May 19 '11 at 10:11
9

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');
Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
6

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"

h_45h
  • 169
  • 4
-3

You can use for getting active article title like this

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

may this help.

Jigar Patel
  • 211
  • 1
  • 5
-5

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!

ybi
  • 1