Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Wordpress conditional statement not working...??

I am trying to add a conditional statement to my sidebar so that a widget I created only appears on certain pages of my site. Here is the code I came up with:

<?php
if ( !($post->post_parent == array(17, 303)) || !(is_page(array(13,321))) || 'news' !== get_post_type() ) {  
/* Add widget */
}  ?>

Open in new window


So to clarify, I only want it to show up when it's not on pages whose parent id's are 17 or 303, OR, not when it's on pages with an id of 13 or 321, OR, not on any pages where the custom post type is "news".

I've been playing around with it for a while but for some reason, I can't get it to work. If I take away all the "OR" statements, each of the statements work on their own. But when they're all together, none of them work. I have also tried writing them in different formats, in different order, and with extra brackets but it didn't seem to do anything.

Any idea what I'm doing wrong?
0
Adam
Asked:
Adam
2 Solutions
 
Jason C. LevineNo oneCommented:
Furio,

Without commenting on the above code yet, are you aware of the Dynamic Widgets plugin?  It basically does what you are are attempting to do with bespoke code :)
0
 
AdamAuthor Commented:
I'll take a look at the widget you mentioned but I was really hoping to figure out what is wrong with my code. Its driving me nuts... :)
0
 
Jason C. LevineNo oneCommented:
(!($post->post_parent == array(17, 303)) || !(is_page(array(13,321))) || 'news' !== get_post_type() )

In plain english:

Page cannot be of parent 17 or 303
Page cannot be 13 or 321
Page cannot have type News

When writing logical statements OR NOT should usually be AND NOT.  You want to exclude all of the events above from being true, not just any one of them.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ujitnosCommented:
You can try conditional widget for wordpress.

http://wordpress.org/extend/plugins/conditional-widgets/
0
 
gwkgCommented:
So to clarify, I only want it to show up when it's not on pages whose parent id's are 17 or 303, OR, not when it's on pages with an id of 13 or 321, OR, not on any pages where the custom post type is "news".
You don't really want to say OR, you want to say AND.  

With OR, if any of the conditionals evaluate to TRUE, the entire conditional is true.  So the way you are stating it, it will show up on any page that isn't 13 or 321.

When writing logical statements OR NOT should usually be AND NOT.  You want to exclude all of the events above from being true, not just any one of them.
That is correct and also why the code isn't working.

You should be using && instead of ||

0
 
AdamAuthor Commented:
Thanks for the help jason1178. I tried the plugin you suggested which actually worked quite well. I also looked into my conditional statement based on your feedback and managed to get that working to. Thanks again!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now