Joomla K2 - 按ID获取文章链接(SEF)

时间:2013-05-21 09:09:42

标签: php joomla joomla-k2

如何通过ID识别文章的 SEF 链接?

我想到这样的事情:

$link = get_link(34); // where 34 is the article ID

<a href="<?php echo $link; ?>">Article link</a>

更新 我的代码是这个(用Rikesh代码更新):

Mysql query: "SELECT id, title, extra_fields FROM xxxxx_k2_items WHERE catid = ".$catid

$n=0; //counter
while($row = mysql_fetch_array($result)){
  $titles[$n] = $row['title'];
  $links[$n] = JRoute::_(ContentHelperRoute::getArticleRoute($row['id'], $catid));
  $n++;
}

好的,现在这会检索/joomla/index.php/currentpage?id=4之类的链接,其中4是正确的ID,但链接不起作用!它走错了一页。我的疑问是:您的代码也适用于K2文章?因为我正在使用K2文章而不是默认的joomla文章。编辑:是的,我已经检查了我的疑问,你的代码链接到ID的Joomla文章,但我正在使用的ID是K2文章! 注意:我需要 SEF 链接

1 个答案:

答案 0 :(得分:1)

你应该包括K2路线

require_once(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');

你应该更新mysql查询和PHP代码,因为你还需要来自k2类别表的信息,这里有一个例子

$db = JFactory::getDBO();
$query = '
    SELECT 
        a.id AS id,
        a.extra_fields AS extrafields,
        a.catid AS catid,
        a.title AS title,
        a.introtext AS introtext,
        a.alias AS alias,
        c.alias AS catalias
    FROM 
        #__k2_items AS a 
        LEFT JOIN #__k2_categories AS c ON ( a.catid = c.id ) 
        WHERE a.published = 1 AND a.catid = '.$catid;
$db->setQuery($query);
$articles = $db->loadObjectList();
$n=0;
foreach ($articles as $article) {
    $titles[$n] = $article->title;
    $links[$n] = K2HelperRoute::getItemRoute($article->id.':'.urlencode($article->alias),$article->catid.':'.urlencode($article->catalias));
    $n++;
}