0

When I want to add a site-wide HTML code (before the closing tag) to each post and page in my site, I always go to my:

  • Wordpress Panel > Customize > Theme Settings > Header/Footer Scripts > Footer Scripts

And paste the code there. Works perfectly fine.

But now I want to add this code only to my posts with certain tags. I tried this (tags are X Y and Z):

function wpb_hook_javascript_footer() {
  if (has_tag ('X' 'Y' 'Z')) { 
    ?>
          // My HTML Code goes here
    <?php
  }
}
add_action('wp_footer', 'wpb_hook_javascript_footer');

But I guess it doesn't work (update keeps loading, never ends)?

Any words of wisdom?

1
  • Were you able to find an answer to this question?
    – jdm2112
    Dec 3, 2020 at 20:37

1 Answer 1

1

Welcome to WPSE. Additional markup for certain posts is more appropriately included in the template for that content type. If these are blog posts, then single.php is the likely template used for display, depending on how your theme is structured.

The likely problem with the code in your question is that you are not using an array to pass multiple parameter values to the has_tag() function. What you have entered is a space delimited list and not a proper PHP array. Using array( 'x', 'y', 'z' ) would be correct.

If you move this to your post template, you are able to eliminate the hook to wp_footer as well.

if ( has_tag( array( 'X','Y','Z' ) ) ) { 
?>
      // My HTML Code goes here
<?php

}

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.