Jump to content

Recommended Posts

Posted

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.

Posted

this would be pretty common

<?php
if ( ($page->template == 'home') || ($page->template == 'basic-page') ) echo "something";
?>
  • Like 2
Posted

.. 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
Posted

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
Posted

The simple PHP OR comparison solution is very common.

The in_array solution is more flexible.

The PW solution owns them all =)

  • Like 5
Posted
if($page->is("template=home|basic-page")) echo "has template";

@Soma....wow - Page IS !  ... better education through forum

  • 2 years later...
Posted

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";
?>
Posted

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
Posted

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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...