0

I have added some custom text at the end of all my articles. Now I want to remove that text if is in a certain category.

For that, I have the following code.

<?php if(in_category('sample-category')) : ?>
<?php else: ?>

(this means I will NOT show my custom text at the end of articles posted in "sample-category") and it works perfectly.

Now I want to do this with 2 or more categories. I have tried anything I know/found but without results, like:

<?php if(in_category('sample-category', 'new-category')) : ?>

Tried by category name, category ID, category slug, but nothing works.

What can I do to exclude the custom text I have added to each single post on 2 or more categories?

Thanks!

1 Answer 1

0

in_category takes 2 parameters, the first being the categories to check, and the second being the post ID.

If we look at the official documentation on wordpress.org we can see these are the parameters that this function can accept:

in_category( int|string|int[]|string[] $category, int|object $post = null )

Taken from: https://developer.wordpress.org/reference/functions/in_category/


We can also see in the official documentation for that function an example of checking more than one category by using an array:

if ( in_category( [ 'Tropical Birds', 'small-mammals' ] ) {
2
  • not sure if I understand. forgot to say i am a complete newbie in coding. so which will be the code? :) Feb 10, 2022 at 14:04
  • @ValiDragomir the first code block is a copy of the documentation showing what parameters it accepts, and the second code block is a copy paste of an example showing in_category being used with 2 categories like you're trying to do. If it's a copy paste solution you're hoping for that requires no understanding though then you've come to the wrong place. My answer only requires beginner level PHP knowledge to understand, specifically basic knowledge of how function calls work, and what an array is.
    – Tom J Nowell
    Feb 10, 2022 at 15:01

Your Answer

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

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