[英]How to add a script to a wordpress page based on category
We recently contracted a digital marketing firm and they've given me 3 tracking scripts to add to our site: 1 for the home page, 1 for product pages and 1 for all other pages.我们最近与一家数字营销公司签约,他们给了我 3 个跟踪脚本以添加到我们的网站:1 个用于主页,1 个用于产品页面,1 个用于所有其他页面。 I have the script being added to the front page and the general product page as expected, but am having a hard time adding it to the product category pages.
我已按预期将脚本添加到首页和一般产品页面,但很难将其添加到产品类别页面。 Using is_category is not the right answer.
使用 is_category 不是正确的答案。
I'm adding them using the following code:我正在使用以下代码添加它们:
add_action('wp_footer','MY_FUNCTION');
function MY_FUNCTION(){
if ( is_front_page() ){
?>
<script>
## Adds homepage script (first script)
</script>
<?php
} elseif ( is_page( '7' ) ) {
?>
<script>
## adds script 2 to the product landing page
</script>
} elseif if ( is_category( 'MY_CATEGORY' ) ):
?>
<script>
## adds script 2 to product pages (they are custom post types, not woocommerce pages))
</script>
</script>
<?php
endif;
}
For the "everything else" scripts I'm using:对于我正在使用的“其他一切”脚本:
if (is_front_page()){
}
if ( is_page( '7' ) ){
}
if ( is_category( 'tours' ) ){
}
else {
<script>
## add script 3 for all other pages
</script>
<?php
}
endif;
}```
Is this a good way go about adding these?
The is_category()
doesn't work for this case. is_category()
不适用于这种情况。 What you can do instead is to get the category id and check if it is the one you need.您可以做的是获取类别 ID 并检查它是否是您需要的。
$cat_id = get_query_var('cat');
if ($cat_id == 5) {
//add script
}
Instead of 5
, use the category id of tours
而不是
5
,使用tours
的类别 id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.