kongondo Posted April 18, 2013 Share Posted April 18, 2013 I have searched the forums and couldn't find an answer. I am wondering how to use "OR" or || or | in my selectors in cases where I don't use either find() or get(). For instance, the following code does not work. Nothing is output. Why is that? if ($page->template=="home|basic-page") {echo "something";} // doesn't work Using | with find() and get() work just fine. Thanks. Link to comment Share on other sites More sharing options...
Macrura Posted April 18, 2013 Share Posted April 18, 2013 this would be pretty common <?php if ( ($page->template == 'home') || ($page->template == 'basic-page') ) echo "something"; ?> 2 Link to comment Share on other sites More sharing options...
teppo Posted April 18, 2013 Share Posted April 18, 2013 .. or optionally this way, especially if there are more than two options: <?php if ( in_array($page->template, array('home', 'basic-page')) ) echo "something"; In your example syntax you're comparing value of $page->template (which here returns template name as a string) with regular PHP string "home|basic-page". You can't include conditional logic within strings, they're essentially just chunks of plain text 5 Link to comment Share on other sites More sharing options...
Soma Posted April 18, 2013 Share Posted April 18, 2013 You're mixing vanilla PHP with PW API. Selectors are PW specific. I don't know you guys know that you can also do this in PW (usually people miss this) if($page->is("template=home|basic-page")) echo "has template"; 13 Link to comment Share on other sites More sharing options...
kongondo Posted April 18, 2013 Author Share Posted April 18, 2013 Thanks all! Link to comment Share on other sites More sharing options...
owzim Posted April 18, 2013 Share Posted April 18, 2013 The simple PHP OR comparison solution is very common. The in_array solution is more flexible. The PW solution owns them all =) 5 Link to comment Share on other sites More sharing options...
Macrura Posted April 18, 2013 Share Posted April 18, 2013 if($page->is("template=home|basic-page")) echo "has template"; @Soma....wow - Page IS ! ... better education through forum Link to comment Share on other sites More sharing options...
Reid Bramblett Posted September 15, 2015 Share Posted September 15, 2015 Though I generally like to be a positive guy, I wish there were also a $page->isnot for certain situations. After all, sometimes 'tis easier to exclude only the two or three unnecessary templates than iterate through all 13 other templates you _want_ to echo the "something." (Also, I couldn't even get the "plain vanilla php" solution to work, either: <?php if ( ($page->template != 'foo') || ($page->template != 'bar') ) echo "something"; ?> Link to comment Share on other sites More sharing options...
Macrura Posted September 15, 2015 Share Posted September 15, 2015 Though I generally like to be a positive guy, I wish there were also a $page->isnot for certain situations. After all, sometimes 'tis easier to exclude only the two or three unnecessary templates than iterate through all 13 other templates you _want_ to echo the "something." (Also, I couldn't even get the "plain vanilla php" solution to work, either: <?php if ( ($page->template != 'foo') || ($page->template != 'bar') ) echo "something"; ?> isnot would be expressed by the false of is if(!$page->is("template=foo|bar")) 2 Link to comment Share on other sites More sharing options...
Reid Bramblett Posted September 15, 2015 Share Posted September 15, 2015 Had I not taken a vow against using emojis, I would be employing the face-palm one right now. Thanks, @Macrura. I was so hellbent on figuring out where to stick the ! into my selector field(s), I forgot I can just "not" the whole string. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now