PHP if statement to apply code snippet only to certain pages

HI Newbie question here, which I think is either something not correct in my php code or my incorrect understanding of how I should do this. I was trying to avoid having to use multiple php templates where possible. So thought maybe I could use some if statements to give certain pages certain php snippets, leave others blank if needed.
Please see my code below…
The problem here is the elseif statements here for ‘Client’ page also produce the snippet on a few of my other pages ‘privacy policy’ and a ‘credits’ page.
Interestingly the homepage is not affected, so maybe just something incorrect with my code?

Great for some advice here, new to php, but very familiar with python. So maybe a language mixup?

<div class="page_area_wrapper">
  <div 
  class="page_area_wrapper_inner_left">

          <?php if($page->isHomePage()):
            snippet('landingvid') ?>

          <?php elseif($page->is('Client')): 
            snippet('portfoliogrid_parent') ?>

          <?php elseif($page->children('Client')): 
            snippet('portfoliogrid_parent') ?>

          <?php else: ?>
            <div></div>

          <?php endif ?>

This won’t work, should be

$page->isChildOf('client')

Make sure to use lower case strings only.

prefect works perfectly! Thanks for the super fast response!

an additional question purely down to nice formatting,

if my else statement should not do anything, is it ok to leave it blank or add an empty div?
in python I would use a not so nice pass, or print something

  1. You don’t need the else at all
  2. Don’t render an empty div unless you need it (e.g. to inject some stuff via JS)
1 Like