簡體   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