Jump to content

Get the pages tree retaining children position


Nicolas
 Share

Recommended Posts

Hi,

I need to make a filter sidebar for a catalogue displayed in a tree like fashion. To get the list of the catalogue root page i've the the following

$view->top_categories = $pages->get(1027)->descendants('template=category, sort=sort');

In the category template the children a sorted by 'None'.

What would be the right selector to display the tree like filter with the categories sorted in the same order as the pages in the admin section ?

catalogue-pages.jpg

catalogue-filter.jpg

Link to comment
Share on other sites

3 hours ago, Nicolas said:

with the categories sorted in the same order as the pages in the admin section

I would use sort=sort in the selector string here - as you already do.

It should work without issues unless you have some sort settings applied in individual templates.
Maybe in some other part of your code you mix things up again.

That $view->top_categories can't be the whole code.
Is that Twig you are using? Are there additional checks or queries?

Link to comment
Share on other sites

Following from @wbmnfktr's suggestion, note that the documentation for sort=sort states "But note that if the results have multiple parents, the resulting order isn't likely to be that useful."

I find that in cases like yours something along the lines suggested in this post is often what I need:

 

  • Like 2
Link to comment
Share on other sites

On 3/19/2024 at 9:52 PM, wbmnfktr said:

I would use sort=sort in the selector string here - as you already do.

It should work without issues unless you have some sort settings applied in individual templates.
Maybe in some other part of your code you mix things up again.

That $view->top_categories can't be the whole code.
Is that Twig you are using? Are there additional checks or queries?

Hi @wbmnfktrThanks for your answer.
I'm indeed using the TemplateEngineLatte module, but I think my issue is more related to the point mentionned by @BillH regarding the "sort=sort" parameter nested pages.

I ended up adopting a recursive approach to solve my problem as shown below :

<?php
// In init.php

$catalogue_root = $pages->get(1027);


$view->categories = getOrderedCategories($catalogue_root->id);

function getOrderedCategories($current_category_id, &$categories = []) {
  
    $current_categories = wire()->pages->get($current_category_id)->children('template=category');

    foreach($current_categories as $category) {
        $categories[] = $category; // populate the reponse with category page object
        getOrderedCategories($category->id, $categories); // proceed with sub categories
    }
  
    return $categories;

}

 

  • Like 2
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...