Solved

Wordpress conditional statement not working...??

Posted on 2011-09-25
6
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
(!($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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:ujitnos
Comment Utility
You can try conditional widget for wordpress.

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

Expert Comment

by:gwkg
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
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.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

10 Experts available now in Live!

Get 1:1 Help Now