最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Get custom text if in Category

programmeradmin3浏览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!

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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' ] ) {
发布评论

评论列表(0)

  1. 暂无评论