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!
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!
Share Improve this question asked Feb 10, 2022 at 13:32 Vali DragomirVali Dragomir 13 bronze badges1 Answer
Reset to default 0in_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' ] ) {