0

I'm trying to return $ad_code if three variables are present, but it doesn't allow the third variable I'm trying to add. I want to exclude a category.

I am trying to use (in accordance with WordPress' "conditional tags")

if ( is_single() && ! is_category( '1293' ) && ! is_admin() )  {
    return prefix_insert_after_paragraph( $ad_code, 2, $content );
}

return $content;

The code (including the code that specifies what $ad_code is) works. Just when I try to add in && ! is_category( '1293' ) it still displays on all posts (not just 1293). I tried using the category name as well, but that also did the same thing.

The third variables I've tried either stop $ad_code from loading on all or on no posts.

  • What about ! is_category( 1293 ) (without single quotes) – czerspalace Oct 26 '15 at 21:16
  • Unfortunately, just the same. It still shows on all posts. – James Frank Haleavy Oct 26 '15 at 21:29
  • ! is_category( 1293 ) checks for any post that is not in category 1293. Is that not what you want? – vancoder Oct 26 '15 at 23:09
  • From the codex, ! means they DON'T put those posts. IE it should display all posts but NOT 1293. This is what I'm trying to make happen. – James Frank Haleavy Oct 27 '15 at 0:57
  • I'll do that, thanks for the heads up Pieter Goosen. I do know that a few of them actually haven't been answered yet, but I'll go recheck them. – James Frank Haleavy Oct 27 '15 at 6:10
1

Your conditional, is_category() is wrong here. is_category() checks whether or not you are actually on a category page. By using the ! operator in conjuction with is_category() and ID 1293, this part of the condition will always return true except when you are actually on the category page for category 1293.

If you need to exclude a certain set of posts from the category 1293, you should use the conditional check, in_category() or has_category() (in essence, they both are excatly the same). So your conditional statement should look something like this:

if (    is_single() // Make sure we are on a single page
     && ! in_category( '1293' ) // Make sure the post does not belong to category 1293
     && ! is_admin() // Make sure this is not an admin page
)  {
    return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
  • Good one, and thank you for your help! That worked and it explained the difference as well. Wish I had enough points to upvote it, but it's checked. – James Frank Haleavy Oct 27 '15 at 6:09
  • No problem. At least you also gain 2 reputation points for accepting answers, so you will get there :-). Enjoy – Pieter Goosen Oct 27 '15 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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