Jump to content

Get children of a page but start at a certain index


Recommended Posts

I am working on a project wich includes a simple event list.

In the backend the structure looks like this. The user can then create event pages under the corresponding month page:

1093807993_Bildschirmfoto2022-08-11um13_05_46.png.b4055c756f12b901f575eec38ba014c3.png

In the frontend I want to display a subnavigation of the upcoming events by month. Starting from the current month! So in this case starting at "August" to September, October, etc. 
This "month pages" only include a title field, to timestamp or whatever.

I need a selector that filters the children of the year starting by an index - the index of the current month. I know that in the example above, August would have an index of 4 (because January - March are missing). But for now we can just ignore that.

This selector here looks correct to me but it only displays "August" not the following months after that (September, October, and so on).

// get all months starting at the current month
$months = $pages->get("template=event-year, title=$year")->children("index>=4");

 

Link to comment
Share on other sites

  • Stefanowitsch changed the title to Get children of a page but start at a certain index

Counter-question: Are you sure you want to have that setup on the backend? In my experience it is much easier to just save all events under one single parent (eg Events / EventItem) and just let every event have one date field. Then you can easily sort by this date field and do filters etc (like showing only future events).

Also changing the date for example would be much easier than changing the date of the event and then additionally having to move the page in the page tree to not get false results (eg an event from august showing up in july)...

  • Like 1
Link to comment
Share on other sites

21 minutes ago, bernhard said:

Counter-question: Are you sure you want to have that setup on the backend? In my experience it is much easier to just save all events under one single parent (eg Events / EventItem) and just let every event have one date field. Then you can easily sort by this date field and do filters etc (like showing only future events).

Also changing the date for example would be much easier than changing the date of the event and then additionally having to move the page in the page tree to not get false results (eg an event from august showing up in july)...

IMHO creating the events on a "month-page-basis" makes the organization much easier. The client has about 120 events per year and some of them are recurring events on a weekly basis. So having all those events listed under a single parent would be too much. After two years there would be 240 events, because once an event as taken place the page is not deleted.

I've got the filtering for upcoming events, events in a single month or past events set up and everything works great so far. 

For example the upcoming event selector looks like this (the event pages itself do have a date field of course).

$events = $pages->find("template=event, date_event>=today, sort=date_event, limit=6");

The month subnavigation is the only thing that is missing so far.

Right now I am thinking of putting a month-page to "hidden" to make it not show up once the month is over. In that case it would not show up at all in the subnavigation.

Link to comment
Share on other sites

First of all, listen to Bernhard, but to answer your question, you can get pages by position using the selector "start": https://processwire.com/docs/selectors/#limit

The number is the zero-based index of the first element you want. The order will reflect that of the page tree unless you specify the "sort" selector.

I haven’t thouroughly checked it out, but you may be interested in the Virtual Parents module: https://github.com/Toutouwai/VirtualParents

  • Like 2
Link to comment
Share on other sites

40 minutes ago, Jan Romero said:

First of all, listen to Bernhard, but to answer your question, you can get pages by position using the selector "start": https://processwire.com/docs/selectors/#limit

The number is the zero-based index of the first element you want. The order will reflect that of the page tree unless you specify the "sort" selector.

I haven’t thouroughly checked it out, but you may be interested in the Virtual Parents module: https://github.com/Toutouwai/VirtualParents

Well, yes and no 🙂 I love (!) Bernhards work but I just stick to my solution for now. If you take a closer look at the page structure in the backend here, I think no one can't deny that this is easier to maintain than having all event pages underneath a single parent. I am thinking about the client who has to do the content work at the end... 
From a technical perspective this is also convenient, because you can view all events of a specific month directly via URL, like www.mysite.com/programm/2022/august/

Otherwise I would have to make use of URL segments to make the filtering happen. 

534203459_Bildschirmfoto2022-08-11um18_10_15.thumb.png.10c20b81b0814e5a45c0e5a790502072.png

  • Like 1
Link to comment
Share on other sites

There's nothing to stop you having a (perhaps hidden) field 'index' on each month page representing the month's number, then your selector above would work (or very nearly work).

If I was doing something like you are, I would probably have some code in _init.php or _ready.php or even at the top of a page template to programmatically create new 'year' and 'month' pages well in advance, creating and populating that 'index' field  as well and probably leave them unpublished so that they can be quickly and easily published when required. I think that would work. Probably.

  • Like 1
Link to comment
Share on other sites

1 hour ago, DaveP said:

There's nothing to stop you having a (perhaps hidden) field 'index' on each month page representing the month's number, then your selector above would work (or very nearly work).

If I was doing something like you are, I would probably have some code in _init.php or _ready.php or even at the top of a page template to programmatically create new 'year' and 'month' pages well in advance, creating and populating that 'index' field  as well and probably leave them unpublished so that they can be quickly and easily published when required. I think that would work. Probably.

Is there a way to set page fields to "hidden" or "readonly" in the backend? Just curious...

However. my personal solution for this was to write a cronjob that runs on the 1st day of every month and simply sets the status of the previous month page to "hidden". In that case those months won't show up in the sub navigation.

The events will later be updated on a 3-month basis, so I think it's okay to let the user create those "month" pages and populate them with events manually.

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...