繁体   English   中英

如何根据类别向wordpress页面添加脚本

[英]How to add a script to a wordpress page based on category

我们最近与一家数字营销公司签约,他们给了我 3 个跟踪脚本以添加到我们的网站:1 个用于主页,1 个用于产品页面,1 个用于所有其他页面。 我已按预期将脚本添加到首页和一般产品页面,但很难将其添加到产品类别页面。 使用 is_category 不是正确的答案。

我正在使用以下代码添加它们:

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; 
}

对于我正在使用的“其他一切”脚本:

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? 

is_category()不适用于这种情况。 您可以做的是获取类别 ID 并检查它是否是您需要的。

$cat_id = get_query_var('cat');
if ($cat_id == 5) {
//add script
}

而不是5 ,使用tours的类别 id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM