admin管理员组文章数量:1332345
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
1 Answer
Reset to default 1Welcome 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
}
本文标签: Adding HTML to posts with certain tags
版权声明:本文标题:Adding HTML to posts with certain tags? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327367a2453994.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论