Forum
Best navigation method for nested categories
Is there an efficient (i.e. perch native) method for generating a navigation system from nested categories?
I'm using a for loop to pull the data from the perch_categories array but fear that this is a massively inefficient hack as it pulls all the data even though I only need a couple of fields?
I assume this approach won't benefit from any kind of perch template caching so I could link to a script that writes the output to a file and include that instead, but I would rather use native Perch functionality if it's available.
Does anyone have any recommendations?
$navlist = perch_categories(array(
'set'=> 'categories',
'skip-template' => true
));
$first_parent = true;
for ($i=0;$i<count($navlist);$i++) {
if (strpos($navlist[$i]['catDisplayPath'], '›')===false) { // › not present so parent category
if (!$first_parent) { echo "</ul>\n</li>\n"; } // only close ul after the first
$first_parent = false;
$cat_slug = $navlist[$i]['catSlug'];
echo '<li><a href="#">'.$navlist[$i]['catTitle']."</a>\n<ul>\n";
} else {
echo '<li><a href="/category/'.$cat_slug.'/'.$navlist[$i]['catSlug'].'">'.$navlist[$i]['catTitle']."</a></li>\n";
}
}
We don't have a really good way currently, but what you're doing there isn't so bad. It should boil down to one simple query that MySQL will cache for you.