0

I've got some code I want to run on every page other than the blog page and the archive page.

If I use

<?php if ( !is_home() )  { ?>

The code correctly doesn't show on the blog page.

If I use

<?php if ( !is_page('archive') )  { ?>

The code correctly doesn't show on the archive page.

But if I use

<?php if ( !is_home() || !is_page('archive') )  { ?>

The code incorrectly shows up on both the pages.

Any suggestion what I'm doing wrong?

4
  • you haven't learned enough php ;) you should use && not || Commented Jul 23, 2014 at 9:40
  • But it's either of those conditions I need, not both? Commented Jul 23, 2014 at 11:15
  • 1
    It's informative to check out the de Morgan's rules: "not (A or B)" is the same as "(not A) and (not B)" and "not (A and B)" is the same as "(not A) or (not B)". So in short: de Morgan rulez ;-)
    – birgire
    Commented Jul 23, 2014 at 13:08
  • 1
    Thank you, that link is great. I had a feeling it was my logic but you don't know what you don't know until you know it right? Commented Jul 23, 2014 at 21:16

1 Answer 1

2

The codex is there to be used and should be your first stop. There is a complete page on conditional tags

To exclude the archive page, use is_archive().

EDIT 1

Mark has a valid point. I didn't understand you well, and maybe myself as well :-)

Your problem is using the || and && operators

Here is your code

 if ( !is_home() || !is_page('archive') )

OK. If you are not on the home page and not on the archive page, both return true, agreed,

 true || true

so your code executes as it should. Now, for instance, we are on the home page, so the check that we are not on the homepage return false, the following happens

false || true

Unlike what you think, your code should execute. This is not a flaw in your code, but how the || operator works. Only one true triggers and executes your code. The same happens when you are on the archive page.

The && operator works like this with your code

 if ( !is_home() && !is_page('archive') )

If you are not on the home page and not on the archive page, both returns true and the code executes.

Now, when you are on either one of the home page or archive page, the one returns true, the other one false. Will your code still executes. The simple answer is NO

For the && operator to trigger, all conditions must return true. Just one false fails your code from executing.

I hope this makes sense

** EDIT 2**

From your comments

Incidentally, would !(is_home() || is_page('archive')) be the same as !is_home() || !is_page('archive') ?

No, it is not. You need to understand what the not (!) **operator does. ! infront of any variable or condition means the opposite of what follows afterwards. So, !is_page() would mean in simple English, "if we are not on a page", where as is_page() translates to "if we are on a page"

So, to look back at what I've explained in EDIT 1, lets look at the logic. I'll abbreviate is_page( 'archive' ) to just is_page()

!(is_home() || is_page('archive'))

On any other page other that home and archive page, !is_home() returns true, and is_page() returns false, executing your code

true || false

On the home page, !is_home returns false, because we are on the home page. is_page() returns false, because we are not on a page.

false || false

So your code does not execute. Now on the archive page. is_page() will return true, while !is_home() will return true as well

true || true

So you are still stuffed. Your code still executes on archive page.

I want to leave you with this. Don't reinvent the wheel. The logic is already there. Go and check the links provided in my answer. You should be using && and not ||, no matter how you look at it, period

10
  • From the reading I've done, is_archive only works on archive posts, not the archive index. I should have been clear here, and I trimmed my code to make it more readable; the line of code is actually: <?php if ( !is_home() || !is_page('exhibition-archive') ) { ?> where 'exhibition-archive' is the archives list page. Commented Jul 23, 2014 at 11:17
  • Also, I have done extensive reading of the codex, and especially the conditional tags page. As stated, individually, each of those checks are working as they should. It's when I put them together that its not doing what it should. Commented Jul 23, 2014 at 11:21
  • What do you mean archive index. is_archive() checks to if an archive page is being displayed. Please see the code link i've provided Commented Jul 23, 2014 at 11:21
  • This should explain what I mean: wordpress.stackexchange.com/questions/22661/… Commented Jul 23, 2014 at 11:23
  • Please see my edit. This should be enough to explain Commented Jul 23, 2014 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.