Jump to content

Sorting renderNav Function


FreeAgent
 Share

Recommended Posts

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

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.

  • Like 3
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...