Solved

Wordpress conditional statement not working...??

Posted on 2011-09-25
6
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

710 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