Solved

Wordpress conditional statement not working...??

Posted on 2011-09-25
6
357 Views
Last Modified: 2012-05-12
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
Comment
Question by:Adam
6 Comments
 
LVL 70

Assisted Solution

by:Jason C. Levine
Jason C. Levine earned 500 total points
ID: 36596075
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
 
LVL 1

Author Comment

by:Adam
ID: 36596118
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 36597110
(!($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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Expert Comment

by:ujitnos
ID: 36597174
You can try conditional widget for wordpress.

http://wordpress.org/extend/plugins/conditional-widgets/
0
 
LVL 31

Expert Comment

by:gwkg
ID: 36601192
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
 
LVL 1

Author Closing Comment

by:Adam
ID: 36814578
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now