Jump to content

OR selector without "find()"


kongondo
 Share

Recommended Posts

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

.. 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 :)

  • Like 5
Link to comment
Share on other sites

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";
  • Like 13
Link to comment
Share on other sites

  • 2 years later...

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

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"))
  • Like 2
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...