I have a child theme with function.php where I am running a script in the header but now I want to tweak it to run only on posts listed under one category and avoid on homepage or other categories. Here is my existing code which I modified but it is firing the script on the entire website.

add_action('wp_head', 'mailchimp_wp_head');
function mailchimp_wp_head() {
        <?php if ( is_category( 93 ) ) :?> 
        <script id="mcjs">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/hidden.js");</script>
        <?php endif; ?>


  • 1
    Your code looks good, so are you sure the category ID is 93? There could also be plugin or other code which is adding the script on your site, so try deactivating plugins. – Sally CJ Jan 13 at 15:22
  • Adding code if ( is_category( 93 ) did have any effect. It is being fired on the entire site like before adding. – howtocode Jan 13 at 15:25
  • 1
    I don't understand, but you mean, "did have" or "did not have"? And as I said, your code should work. – Sally CJ Jan 13 at 15:32
  • This is my current live code add_action('wp_head', 'mailchimp_wp_head'); function mailchimp_wp_head() { ?> <script id="mcjs">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","chimpstatic.com/mcjs-connected/js/users/hidden.js");</…> <?php } I tried modifying it as in the question but it still fires on every category – howtocode Jan 13 at 15:34
  • Then it sounds like a caching issue. Try clearing your site and browser caches. – Sally CJ Jan 13 at 15:40

run only on posts listed under one category

I highlighted that "posts" because if so, then the conditional tag you should use is in_category() and not is_category() which is for category archive pages (e.g. example.com/category/foo) — so for example, is_category( 93 ) checks if the current archive page is for the category (with the ID of) 93, whereas in_category( 93 ) checks if the current post is in the category 93.

So try with:

<?php if ( is_single() && in_category( 93 ) ) :?> 
<script id="mcjs">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/hidden.js");</script>
<?php endif; ?>
  • It is still running the script in every category posts – howtocode Jan 13 at 16:27
  • I will try to explain in layman's language. There are two categories CAT-A (ID 92) and CAT-B (ID 93). Each Category has 5 posts. I want to restrict firing script CAT-B (ID 93) but it is firing on both categories and homepage even after using your new code mentioned above using is_single and in_category – howtocode Jan 13 at 16:36
  • Then looks like you just need some PHP condition there.. try is_single() && ( in_category( 93 ) && ! in_category( 92 ) ) – Sally CJ Jan 13 at 16:39
  • So did that work? If so and if my answer answered your question, please consider marking my answer as "correct" - just tick the check-mark next to my answer. – Sally CJ Jan 13 at 17:01
  • 1
    Yes, the original code-shared by you with is_single && in_category works. There was a mistake on my part initially as the same code was in widget so it was firing. Thanks for your help. – howtocode Jan 14 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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