Solved

Wordpress conditional statement not working...??

Posted on 2011-09-25
6
358 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php simple error message 4 23
ajaxSubmit is giving me an error 1 37
comma true 6 32
Change background color in td if any value is 1 7 9
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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