Hi Michael,
My use case was for a parent and one sub-level, but I expect the solution to scale across multiple levels.
The solution uses two standard page assets: Prev-Next Overview and Prev-Next Siblings. The former is nested in the top-level page, and the latter is nested in the child pages.
To get the first child of the parent page, I get an array of child assets (Type 2, in this case) and keep the first item in the array. Then I print it using the %globals_asset_name_linked:nnnnn%
keyword.
<script runat="server">
var list = %frontend_asset_children_link_type_2^array_slice:1:1%;
var nextAssetID = list[0];
print('<div id="prev-next-nav">');
print('<p class="float-right">Next: %globals_asset_name_linked:' + nextAssetID +'%</p>');
print('</div>');
</script>
Getting the the sibling navigation is a bit more complicated. This time, we get an array of sibling assets (the children of the frontend asset’s parent) and remove the first item (the parent asset’s Page Contents container). Then we set up some variables and add the ID of the frontend parent asset to the start of the array.
Iterating through the array, we check if the current item is the frontend asset ID. If it is, we get the previous asset ID, since it always exists (remember, we added the frontend parent asset ID to the array, so we won’t go out of bounds).
To get the next sibling asset, we need to check our position in the array. Remember, we need to allow for the current frontend asset being the final sibling asset, which is why we use list.length - 1
.
After this, we just need to print the hyperlink, checking for a null
value, in case we are on the final sibling asset.
<script runat="server">
var list = %frontend_asset_parent^as_asset:asset_children_link_type_2^array_slice:1%;
var currentAssetID = %frontend_asset_assetid%, prevAssetID, nextAssetID;
list.unshift(%frontend_asset_parent%);
for (i = 1; i < list.length; i++) {
if (list[i] == currentAssetID) {
prevAssetID = list[i-1];
nextAssetID = (i < list.length - 1) ? list[i+1] : null;
break;
}
}
print('<div id="prev-next-nav">');
if (prevAssetID) {
print('<p class="float-left">Previous: %globals_asset_name_linked:' + prevAssetID + '%</p>');
}
if (nextAssetID) {
print('<p class="float-right">Next: %globals_asset_name_linked:' + nextAssetID + '%</p>');
}
print('</div>');
</script>
Happy to help if you have any questions.