Jump to content

Check Position in Page Tree


Fuzzy
 Share

Recommended Posts

Hi,

is there a possibility to check if  a page is before/after another page in the admin page tree?

Example:

> Categories
>> In Progress
>> Completed
>> Waiting for Approval

What I'm looking for is an if statement that checks whether "Completed" is before/after "In Progress" in the page tree. Checking the ID is not an option as "In Progress" might have been created after "Completed".

Thanks in advance for your help!

Link to comment
Share on other sites

You can check $page->prev() and $page->next() to find siblings matching a selector. You need of course some criteria to identify the "Completed" page – if using an ID is not an option, you could use the name or title of the page.

Here are some code examples (those assume $page is the "In Progress" page):

// will be true if the "Completed" page is somewhere before the current $page
$CompletedIsBefore = $page->prev('title="Completed"')->id !== 0;

// will be true if the "Completed" page is somewhere after the current $page
$CompletedIsAfter = $page->next('title="Completed"')->id !== 0;

// will be true if the "Completed" page comes directly before the current $page
$CompletedIsPreviousPage = $page->prev()->matches('title="Completed"');

See $page->prev() and $page->next() in the API reference.

  • Like 1
Link to comment
Share on other sites

So long as you don't have a custom sort setting on the parent template or parent page (i.e. the pages are just sorted according to the default which is "Manual drag-n-drop") then you can compare the "sort" value of the pages.

if($sibling_1->sort > $sibling_2->sort) {
	// $sibling_1 is after $sibling_2 in the page tree 
}

 

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