Adding HTML to posts with certain tags?

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

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?

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?

Share Improve this question asked Jun 22, 2020 at 14:33 user190411user190411 1 1
  • Were you able to find an answer to this question? – jdm2112 Commented Dec 3, 2020 at 20:37
Add a comment  | 

1 Answer 1

Reset to default 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

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327367a4423019.html

相关推荐

  • Adding HTML to posts with certain tags?

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

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信