-
- 12 Posts
Hi,
Just want to know if it’s possible with Modx to generate correct navigation for categories/subcategories through the admin. I played with the script only a little bit and what I see with the default demo version is that the script seems to suffer from the same problems as the current script I’m using: getting the navigation to display like you want. I need to create literally thousands of categories and I certainly don’t want them ALL showing up on every page. So is there a way to simply (SIMPLY being the key word here) make it so that 1.)categories hide their subcategories till clicked, and (2) when you click on subcategories have them display their parent structure when pages are loaded? ’Hope I’ve been clear on this, what I’m looking for.
Thank you.
I’m not aware of a default menu builder that does this, but it sounds like it’d be relatively easy to build.
Ryan Thrash, MODX Co-Founder
Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
-
- 3,250 Posts
Well, that snippet is by Adam who is part of our core team. Additionally, we’ll help with just about any snippet here so no worries about support. Also, we try to not ignore existing snippets with new releases so its doubtfull it would become obsolute anytime soon.
-
- 12 Posts
Okay the snippet seems to work fine except for one annoying thing: an extra nav icon that precedes the category list on the first page and that’s inserted between the last couple of categories listed on all other pages. I’m sure it’s something incredibly simple but I’m just not seeing it at the moment. I know it’s not the css because I tried the snippet in a test template that had no other code or css reference and the same problem was occurring. Is it just one too many breaks or something my editor is making or something in the snippet code itself that needs to be edited? This is the snippet code exactly as I’m using it:
// PSUEDO CONSTANTS //
$page_fields = ’id,pagetitle,description,published,deleted,isfolder, hidemenu’;
// INITIALIZE VARIABLES //
$parents = array();
if(!$doc_id) { $doc_id = $modx->documentIdentifier; } // If doc_id not set use the current doc
$page = $modx->getPageInfo($doc_id,0,$page_fields);
// If the current page is a folder add it to the parent array
if($page[’isfolder’]) {
$parents[$page[’id’]] = $page;
}
// Loop through each parent to the current page
// if it’s published, not deleted and show in menu is set, add it to the parent array
while($page = $modx->getParent($page[’id’],0,$page_fields)) {
if($page[’published’] && !$page[’deleted’] && !$page[’hidemenu’]) {
$parents[$page[’id’]] = $page;
}
}
// Reverse the array of parents so we can start at the top instead of the bottom
$parents = array_reverse($parents,true);
// Loop through each parent and add it to the html
foreach($parents as $parent) {
if($parent[’published’] && !$parent[’deleted’] && !$parent[’hidemenu’]) {
if($parent[’id’] == $doc_id) {
$parent_classes = "$parent_class $current_class";
} else {
$parent_classes = "$parent_class";
}
$parents_html .= "<li class=\"$parent_classes\"><a href=\"[~$parent[id]~]\">$parent[pagetitle]</a></li>\n";
}
}
// Get the children of the last parent
$children = $modx->getActiveChildren($parent[’id’]);
// Loop through each child and add it to the html
foreach($children as $child) {
if($child[’id’] == $doc_id) {
$child_classes = "$child_class $current_class";
} else {
$child_classes = "$child_class";
}
$children_html .= "<li class=\"$child_classes\"><a href=\"[~$child[id]~]\">$child[pagetitle]</a></li>\n";
}
// Complile the HTML into one HTML string
$html = <<<EOD
<ul>
$parents_html
<li class="$children_class">
<ul>
$children_html
</ul>
</li>
</ul>
EOD;
// Return the HTML
return $html;
Sorry if it’s a dumb question but I’m new to all this....
-
- 1,495 Posts
I don’t see the code is inserting something to the <ul> or <li>, other than the content of pagetitle. Do you have a link where we can look into?
I really don’t approve of that sidebar, the way it’s all done in one big <ul>. Each section should be in its own <div class="sideblock"> container, and the <ul> tag should be used for real lists, like menus. That would avoid the problem you’re having. I would have to see your template and chunks to be sure where that extra <li> tag is coming from, from the source it looks like you are using the wrong startDoc id for the menu snippet call, but it’s hard to be sure.