admin管理员组文章数量:1389844
I am trying to add the script to the particular Product Category page, but the code is not adding it to the category.
Page (working):
function wpb_hook_faq_javascript() {
if (is_single ('8') || is_page ('8')) {
?>
<script type="application/ld+json">
</script>
<?php }
}
add_action('wp_footer', 'wpb_hook_faq_javascript');
Product Category (not working):
function wpb_hook_faqtocategory_javascript() {
if ( in_category( 'category-name' )) {
?>
<script type="application/ld+json">
</script>
<?php }
}
add_action('wp_footer', 'wpb_hook_faqtocategory_javascript');
But the above code is not working.
Product Category URL:
wp-admin/term.php?taxonomy=product_cat&tag_ID=9&post_type=product
I am trying to add the script to the particular Product Category page, but the code is not adding it to the category.
Page (working):
function wpb_hook_faq_javascript() {
if (is_single ('8') || is_page ('8')) {
?>
<script type="application/ld+json">
</script>
<?php }
}
add_action('wp_footer', 'wpb_hook_faq_javascript');
Product Category (not working):
function wpb_hook_faqtocategory_javascript() {
if ( in_category( 'category-name' )) {
?>
<script type="application/ld+json">
</script>
<?php }
}
add_action('wp_footer', 'wpb_hook_faqtocategory_javascript');
But the above code is not working.
Product Category URL:
wp-admin/term.php?taxonomy=product_cat&tag_ID=9&post_type=product
Share
Improve this question
edited Mar 24, 2020 at 0:06
WordPress Speed
2,2833 gold badges19 silver badges34 bronze badges
asked Mar 22, 2020 at 8:35
Rahul KumarRahul Kumar
2074 silver badges20 bronze badges
1 Answer
Reset to default 3in_category()
should be used inside The Loop (unless if you pass a post ID as the second parameter like in_category( 1, 1 )
) and only for the default/built-in category
taxonomy.
For custom taxonomies like the product_cat
in your case, you should use has_term()
; however, if you're checking if the current request/page is a taxonomy archive page, you should instead use is_tax()
:
function wpb_hook_faqtocategory_javascript() {
if ( is_tax( 'product_cat', 9 ) ) { // 9 is the term ID, but you can use the slug/name
?>
<script type="application/ld+json">
</script>
<?php
}
}
Or in WooCommerce (you're using WooCommerce, right?), you can use is_product_category()
, but you'll need to know the term slug:
function wpb_hook_faqtocategory_javascript() {
if ( is_product_category( 'term-slug' ) ) {
// ... your code here ...
}
}
本文标签: categoriesHow to add custom script to the particular Product Category page
版权声明:本文标题:categories - How to add custom script to the particular Product Category page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744645426a2617373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论