Jump to content

PHP if condition question


MarcC
 Share

Recommended Posts

Why doesn't the first condition work? The if statement evaluates true on a child of home, but I don't want it to. Thanks!

if (($page->parent->name != 'home') && ($page->template->name == 'basic-page') && $page->numChildren)
Link to comment
Share on other sites

If using $page->parent in a string context, it's actually going to resolve to the ID. But I think it's better to be clear about what property you are checking. I recommend doing this:

if($page->parent->id > 1) 

The homepage always has an ID of 1 and that's a factor you can count on in PW.

Link to comment
Share on other sites

I'm still not sure that exactly you're trying to do. :)

You say it returns true when on a child of home but you don't want to? But that's what you're trying to originally want in your code, no? Or do you mean false? Do you have a page "home" ? Or do you mean the root?

Link to comment
Share on other sites

OK, my logic was all wrong :-) But those are helpful tips.

I have a bunch of pages all with the basic-page template.

Inside of basic-page there's a routine that displays all of a page's children below the body (like Ryan's example site).

However, ONE of those pages already has its children displayed ABOVE the body. So it needed an exemption; didn't want two child-menus on the page. Also didn't want to make yet another template file for this.

So I ended up with:

if (($page->id != 1014) && ($page->template->name == 'basic-page') && $page->numChildren) {
                
                echo "<hr />\n<ul class='section-menu'>\n\t";
                
                foreach($page->children as $child) {
                    echo "<li><a href='{$child->url}'>{$child->title}</a></li>\n";
                }
                
                echo "</ul>"; 
                
                }

...and that works fine.

Let me know if you see any flaws in my thinking. Still trying to learn the art.

Link to comment
Share on other sites

Ah that makes sense now :D

That's ok I think. You could also just use $page->template == "basic-page" as it returns the name and if compared to a string works fine.

An alternative would be to have a checkbox field on the template you could check on the desired page and not have the subpages listed. So you don't have to worry about id's.

Then going with something like this.

<?php
if( $page->not_pagelist != 1 && $page->template == "basic-page" && $page->numCildren ) {
   ...
}
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...