Page 1 of 1

How to get URL to article from ID?

Posted: Mon Nov 26, 2007 4:37 pm
by ShiRaTo
Hi all,

I'm using Joomla 1.5
I just wondering how can I create a link to article link this -> index.php?view=article&catid=10&id=20&option=com_content&Itemid=30
if I know only id of article (in this example it's =20)

I try to write code like this
">link

but It's not use the template that I assign for that article..  it's only work if I add more parameter, the Itemid=30 to it..  but It's cannot hard-code..  it's depend..

so..  are there any API or how to make a link if I know only article id?

Thank you very much,


ps. I can't query database to find it..  because It will be very slow since on that page I want to show a lot of articles..

Re: How to get URL to article from ID?

Posted: Fri Nov 30, 2007 1:27 pm
by ShiRaTo
now my problem is solved by this.

Code: Select all

include dirname(__FILE__).'/../../components/com_content/helpers/route.php';

// Search Joomla to get Category ID of each article.
$db = &JFactory::getDBO();
$idList = "";
foreach ($resArticle->docs as $no => $doc) {
	$idList .= $doc->sums["cityguidearticleid"].",";
}
$idList = substr($idList, 0, -1);
$db->setQuery('SELECT id,catid FROM #__content WHERE id in ('.$idList.')');
$rows = $db->loadObjectList();

foreach ( $rows as $row ) {
	$articleLink[$row->id] = $row->catid;
}

and when I want to show..
<a href="<?php echo ContentHelperRoute::getArticleRoute($articleId, $articleLink[$articleId]); ?>">Article <?php echo $articleId; ?></a>

explain:  I query DB for all article I have to get category ID list...  when I want to show I call ContentHelperRoute::getArticleRoute and send article id with category id...


I'm not sure this's the best way or not..  but I use 1.5RC3 and I can't find how to do this...
I've read JApplication class that says "Deprecated, use ContentHelper::getItemid instead."  but I think that'll query db everytime?  or not?  I can't find that source code in google or search forums here

Re: How to get URL to article from ID?

Posted: Sun Jun 08, 2008 9:09 am
by seanray
I believe you are doing the right thing here. When I look at the source code of Joomla and try to find out how they handle artitcle link in module "Latest News", they use the same way as your mention in above, see below:
$lists[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));

Re: How to get URL to article from ID?

Posted: Sun Jun 08, 2008 9:16 am
by ah72
Hello,

you can link easily to articles with this

Code: Select all

http://yoursite.com/index.php?option=com_content&view=article&id=20
or even simpler

Code: Select all

http://yoursite.com/index.php?option=com_content&id=20
Regards

Re: How to get URL to article from ID?

Posted: Sun Jun 08, 2008 10:14 am
by seanray
hi ah72,
I think your method works as a general, but what we need here is a Search Engine Friendly URLs.

thanks,
Ray.