Links to section/category from a content item in Joomla

For version 1.0.x: normally, Joomla’s mosPathWay() and module would take care of generating links to sections and categories, but in case you ever needed to do it yourself:

        // TODO: You need someway to get $rid, which is the ID of the content item.
        // I usually get this in the main content.html.php page, so I use $row->id

        // The SQL to get the section and category for an article
        $sql = "SELECT c.title ctitle, c.id cid, s.title stitle, s.id sid FROM #__content co INNER JOIN #__categories c, #__sections s WHERE (co.id=$rid) AND (c.id=co.catid) AND (s.id=co.sectionid)";
        $database->setQuery($sql);
        $result = $database->loadRow();

        if ($result != null) {
                // Section page
                $sid = $result[3];
                $stitle = $result[2];
                $slink = sefRelToAbs("index.php?option=com_content&task=section&id=$sid");

                // Category page
                $cid = $result[1];
                $ctitle = $result[0];
                $clink = sefRelToAbs("index.php?option=com_content&task=category&sectionid=$sid&id=$cid");

                // Write out links
                $relatedSecCat = "Related articles: <a href='$slink'>$stitle</a> > <a href='$clink'>$ctitle</a>";
        }
        else $relatedSecCat = "";
        echo $relatedSecCat;

No comments yet

Leave a reply