1
votes

I have a client built site that I am adding some functionality too - I don't usually develop using Wordpress. They have built pages using Visual Composer to display posts from varying categories

If the post is within a certain category 'Deals' I want to do stuff … non-working code (in functions.php) below:

function deals () {
    if ( in_category('Deals') ) {
        echo '<style>.entry-thumb{display: none !important;}</style>';
    }
}

Calling function from within child theme page template.

Any help would be great thanks

2

2 Answers

1
votes

You can check if current post is in category by using

if( has_category('Deals') ) { // do stuff here }

If $post global variable is set has_category('Deals') will be ok. Otherwise you will need to pass post ID as second parameter. https://developer.wordpress.org/reference/functions/has_category/

P.S. If you are calling it in a loop it looks like you are trying to echo the same inline css multiple times. This will hide all .entry-thumbs regardless of the category. So it may be better to add a class to your deal posts and then use something like .deal .entry-thumb{ display: none; } in your style.css.

0
votes

You should try is_category() function like this:

function deals () {
  if ( is_category('Deals') ) {
   echo '<style>.entry-thumb{display: none !important;}</style>';
  }
}