Jump to content

if sibling


onjegolders
 Share

Recommended Posts

Sorry, should be something basic but can't seem to find the answer in here.

If I want a section with a header ie: (Other news articles) to display only if the page has siblings, how would I go about checking that in an if statement?

I have tried:

if ($page->siblings)
if ($page->siblings())
if ($page->siblings->id)
if ($page->siblings.count > 0)

I find myself always guessing at the different ways on testing whether or not something is empty or does not exist.

Is there a general rule? This might help me clarify when to use which of the above!

Thanks guys.

Link to comment
Share on other sites

With siblings, since it also returns always the page itself (where it is called), $page->siblings will always return at least one page object. SO you would have to check if the count is greater than 1. Not?

In this case, you would do something like this to check if the current page has any siblings.

if( count ( $page->siblings ) ) > 1 ) {
...
}

An alternative would be to use the count method for Page Array in PW.

if( $page->siblings->count() > 1 ) {
...
}

As for checking if a field is empty. The if condition works with different return values type false|0|null|empty as false. So if something returns 1 or more it will be true.

Usually a if($page->field) will work with textfield and also checkbox (0=not selected or 1 selected) and more.

To check if a field exists is a little different. It won't trow an error if the field doesn't exists in a if condition in PW. Also this is not something you'll be doing most of the time but you still can.

php isset will return 1 if the property exists (even if empty) in the page object.

if( isset( $page->field ) ){
  ...
}
  • 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

×
×
  • Create New...