Hey folks,
I’m using the fantastic Structure module to organize a site’s content, and I need to be able to loop over the children for the given page and display a single custom field for them (sidebar shows title w/ link and a short description for all the children pages).
There’s no good way to do this built into Structure, and I’m curious if anyone has advice on a direction to go to accomplish this. I took a look at Structure’s source to see if I could duplicate something like the sub_nav function to output custom fields along with title and URL, but I can’t figure out how to get custom fields associated with the Structure-provided data.
Any suggestions are welcome.
I actually ended up hacking the core Structure module to do this. Code example below.
mod.structure.php around line 834 add:
//
// Child IDs function
// Returns a string of IDs for a given parent
//
function child_ids() {
global $IN, $PREFS, $DB, $TMPL;
$site_pages = $PREFS->ini('site_pages');
// Fetch our parent ID, or if none default to the current page
$parent = $TMPL->fetch_param('entry_id');
if (!$parent) {
// Find the parent in the site pages array using URL
$site_pages = $PREFS->ini('site_pages');
$parent = array_search($IN->URI, $site_pages['uris']);
}
// Grab the delimiter, or default to a pipe
$delimiter = $TMPL->fetch_param('delimiter');
$delimiter = $delimiter ? $delimiter : '|';
// There's bound to be a better way to do this using Structure's pre-existing logic
$results = $DB->query("SELECT entry_id FROM exp_structure WHERE parent_id = '$parent' ORDER BY lft ASC");
$entries = array();
if ($results->num_rows > 0) {
foreach($results->result as $row) {
$entries[] = $row['entry_id'];
}
}
return implode($delimiter, $entries);
}
//
Usage:
{exp:weblog:entries dynamic="off" fixed_order="{exp:structure:child_ids}"}
{!-- output info here --}
{/exp:weblog:entries}
You can specify two things explicitly: the entry_id for the top-level parent whose children you want to display (defaults to current page, assuming it’s a Structure page), and the delimiter for the string of IDs (defaults to “|”):
{exp:structure:child_ids entry_id="3" delimiter="|"}
There’s probably issues with it, but it’s been working great so far. I’ve submitted the idea to Travis (the Structure author) and he said that they’re likely to have something similar (if not exactly alike) in Structure 2.0. No idea when that’s likely to come out, though.
If you use the hack and find a problem with it, let me know so I can fix my own sites. 😊
Ah. I’m actually teamed up with Travis on the module dev team and recognize this code now. It should be implemented in an upcoming version Structure, but rewritten to utilize it’s standard functions etc… possibly with some more parameters to control its output as well.
As far as tapping into those entries’ custom fields… i can look into that and add it as a feature request. Not a bad idea – we would need a few more examples of where that would be useful in order to ensure flexibility.
One question tho – does your tag usage work without parse=”inward” in your entries tag?
oh, and can’t tell ya about Structure 2 😉
One question tho – does your tag usage work without parse=”inward” in your entries tag?
That’s a good question; I actually haven’t tested that. My usage currently is passing the IDs to an embedded template (shortened it down to the code above for simplicity’s sake, but now you mention it probably would have issues).
Good afternoon,
We have been running structure since we launched our site. This morning I attempted to access Structure from the Modules tab and I get a blank page. It appears to work fine from the Publish/Edit tabs.
I know we last accessed the module on 10/8…and as far as I have been able to determine there have been no changes to our environment. I am at a loss, any help is appreciated.
Jorge
UPDATE - I moved this post to the main thread. Sorry about the erroneous post.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.