1

I want to display the ad only in specific categories. Can anyone help me here's the code to display the ad on all single posts/

<?php get_template_part( 'template-parts/ads/below-head' ); ?>

1 Answer 1

0

This will display the ad on all posts belonging to the categories listed in the array:

<?php
global $post;
if ( in_category( array('news', 'cats', 'dogs'), $post->ID ) ) {
    get_template_part( 'template-parts/ads/below-head' );
} ?>
4
  • I edited it to work outside the Loop, give it a try. To be clear, you want to display the ad on posts, not category archives right? Commented Dec 1, 2021 at 14:19
  • It was working, I'm sorry I've cleared the CDN cache.
    – Kumar
    Commented Dec 1, 2021 at 14:19
  • Hi, If I don't want to display the ad in these categories (news, cats, dogs? Can you provide the code, please?
    – Kumar
    Commented Dec 5, 2021 at 16:19
  • Certainly, just add an exclamation mark like so: if ( !in_category( array('news', 'cats', 'dogs') ) ) Commented Dec 5, 2021 at 20:28

Your Answer

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

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