listing all pages alphabetically by title

Post Reply
jayoh
Jr. Bludit
Posts: 2
Joined: Thu Jan 31, 2019 5:00 pm

i'm using the code from the manual to list all pages, but i'd like to sort them alphabetically.

using sort() on the array sorts it alphabetically by URL path (i believe). how can i sort by title?

Code: Select all

<?php
	    // Page number of the paginator, the first page is 1
	    $pageNumber = 1;

	    // The value -1 tell to Bludit to returns all the pages on the system
	    $numberOfItems = -1;

	    // Only get the pages with the satus published
	    $onlyPublished = true;

	    // Get the list of keys of pages
	    $items = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);

      //alphabetical
      sort($items);


	    foreach ($items as $key) {
	        // buildPage function returns a Page-Object
	        $page2 = buildPage($key);
 
	        // Print the page title
		?>
		<li class="nav-item">
			<a class="nav-link" href="<?php echo $page2->permalink(); ?>"><?php echo $page2->title(); ?></a>
	    </li>
	<?php } ?>
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

The following works:

Code: Select all

<?php
    // Page number of the paginator, the first page is 1
    $pageNumber = 1;

    // The value -1 tell to Bludit to returns all the pages on the system
    $numberOfItems = -1;

    // Only get the pages with the satus published
    $onlyPublished = true;

    // Get the list of keys of pages
    $items = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);

    $new_list = array();

    foreach ($items as $key) {
        // buildPage function returns a Page-Object
        $page = buildPage($key);

        $title = $page->title();
        $permalink = $page->permalink();

        $new_list[] = "$title,$permalink";
    }

    sort($new_list);

    foreach ($new_list as $page) {

        $permalink = strstr($page, ',');
        $permalink = trim($permalink, ',');

        $title = strstr($page, ',', true);
        
        ?>

        <li class="nav-item">
            <a class="nav-link" href="<?php echo $permalink; ?>"><?php echo $title; ?></a>
        </li>

<?php } ?>
Perhaps there is a more elegant solution...
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
jayoh
Jr. Bludit
Posts: 2
Joined: Thu Jan 31, 2019 5:00 pm

works! i'm not enough of a developer to create a more elegant solution -- so this is perfect for me. thank you. :D
Post Reply