Checking if a page is not a draft and include its content

Hello,

I’m migrating from V2 to V3 and try to display the content of a project page on the homepage if the page is listed.

In V2 I was doing:
<?php if(page('projets')->isVisible() && page('projets')->hasVisibleChildren()): ?>

in V3 I tried:
<?php if(!page('projets')->isDraft() && page('projets')->hasListedChildren()): ?>

or

<?php if(page('projets')->exists() && page('projets')->isListed() && page('projets')->hasListedChildren()): ?>

But it throw the error Call to a member function exists() on null

I think it’s normal as the page does not really exists (just in draft state) but I don’t know if there is a solution to display this page only if it’s not a draft. I tried with find() but don’t succeed.

How can I “select” draft pages so my client can load/unload content of certain sections of his website?

Thank you.

The page() helper only fetches published pages, so checking if it is a draft is not necessary. However, before you check if the page has children, you should make sure you have a page. So your code should look like this:

<?php if (($page = page('projects')) and $page->hasListedChildren()): ?>
<!-- do something -->
<?php endif ?>

Thank you very much!
(doesn’t work as first as I past the code with $page = page('projects') in english instead of $page = page('projets') in french :slight_smile:)

Oh, sorry, missed the French spelling… But at least it didn’t throw an error, that’s why we introduce the check if the page exists.

No problem. And to achieve exactly the same behavior than in V2, the code is:

<?php if (($page = page('projets')) and $page->isListed() and $page->hasListedChildren()): ?>
<!-- do something -->
<?php endif ?>