FreeAgent Posted September 26, 2015 Share Posted September 26, 2015 I'm running into issue trying to sort the renderNav function. Here is the default code... function renderNav(PageArray $items) { // $out is where we store the markup we are creating in this function $out = ''; // cycle through all the items foreach($items as $item) { // render markup for each navigation item as an <li> if($item->id == wire('page')->id) { // if current item is the same as the page being viewed, add a "current" class to it $out .= "<li class='current'>"; } else { // otherwise just a regular list item $out .= "<li>"; } // markup for the link $out .= "<a href='$item->url'>$item->title</a>"; // if the item has summary text, include that too if($item->summary) $out .= "<div class='summary'>$item->summary</div>"; // close the list item $out .= "</li>"; } // if output was generated above, wrap it in a <ul> if($out) $out = "<ul class='nav'>$out</ul>\n"; // return the markup we generated above return $out; } I'm wanting it to order the pages from newest to oldest. The code above list oldest to newest. Can anyone help me out with this? Link to comment Share on other sites More sharing options...
horst Posted September 26, 2015 Share Posted September 26, 2015 Hi FreeAgent, the renderNav function only processes the items within the PageArray just in the order how you have created the PageArray. So, you don't need to change the renderNav function, you need to sort the items of the PageArray in the order you want them to be displayed. First thing is: how have you managed the order in the admin pagetree (in backend)? have you set the blog children to be sorted newest first? ("-created") If not, you I think you should set it this way and I expect that this order will be used to build the PageArray what is passed to the renderNav function. Changing the sort order for a pages children can be done by opening the relevant page for editing, selecting its children tab and go down to the field "sort settings". --- --- --- And going one step further: To ignore the selected sort order of the Admin-PageTree and to sort items of PageArrays by the API is also possible and used very often. // here you collect all children of the current page $pageArray = $page->children("sort=-created"); // here you first "get" the blog-page (the parent / rootparent of all blog entries) and then collect all of its children $pageArray = $pages->get("name=blog")->children("sort=modified"); // with the selector string you can, (besides others), also pass a param for the sorting order // "find" something different: $pageArray = $pages->find("template=news, sort=-modified"); // it is possible to sort by other things, please refer to the docs . Also useful is to refer to somas awesome cheatsheet. 3 Link to comment Share on other sites More sharing options...
FreeAgent Posted September 27, 2015 Author Share Posted September 27, 2015 Thank you so much, Horst! You've went above and beyond helping me out. I wish I could buy you a beer. I'm great with frontend code, but I get lost in PHP real quick. I'm picking up a book today so I can learn a few things. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now