2017-08-17 62 views
1

上午所有,上WooCommerce定製產品頁面有條件地將自定義內容

我創建的代碼片段從圖像後拉的相關信息,並顯示下載的3個選項。

我已經更改了網站的設計以便在Woocommerece的產品上運行,我如何調整我的代碼片段以允許此功能工作?

<?php if(in_category(531)) { ?> 
       <?php if (current_user_can('manage_options')) { ?> 
       <p> 
       &darr; Download Image<br /> 
       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'web', true); 
       echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url')?>/img/downloadForWeb.png" alt="download for web" /></a><br /> 

       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'print', true); 
       echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForPrint.png" alt="download for print" /></a><br /> 

       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'', true); 
       echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br /> 
       </p> 
       <?php } else { ?> 
       <h2>Special permission required</h2> 
       <p>In order to use this image you need special permission from the admin, please fill in the form below and we'll get back to 
       you as soon as possible...</p> 
       <?php echo do_shortcode('[contact-form 11 "special permission"]') ?> 
       <?php } ?> 

       <?php } else { ?> 
       <p> 
       &darr; Download Image<br /> 
       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'web', true); 
       echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url')?>/img/downloadForWeb.png" alt="download for web" /></a><br /> 

       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'print', true); 
       echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForPrint.png" alt="download for print" /></a><br /> 

       <a href="<?php $image_id = get_post_thumbnail_id(); 
       $image_url = wp_get_attachment_image_src($image_id,'', true); 
       echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br /> 
       </p> 
       <?php } ?> 
+0

@LoicTheAztec這裏是一個鏈接,我想它關鍵詞下顯示。 http://www.redrocketgraphicdesign.co.uk/test/FIRST/product/firststudent1/ –

+0

@LoicTheAztec這是一個產品頁面。當它被設置爲一個帖子,它就像這樣https://snag.gy/jCDgqK.jpg 你點擊你想要的選項,它下載相關的圖像,上面的代碼是它如何工作的職位。我想切換到在產品頁面上工作。 –

回答

1

的正確方法似乎使用woocommerce_before_add_to_cart_form動作鉤子鉤住的自定義功能,嵌入你的代碼,並在你的產品頁面來顯示它。

但你必須確保ID 511產品類別(而不是一個正常的WP類別。如果沒有,你將不得不創建和名稱來取代這個ID, 。蛞蝓或您的新產品類別的ID

這應該是你的代碼:

add_action('woocommerce_before_add_to_cart_form','my_custom_product_content', 1, 0); 
function my_custom_product_content(){ 
    global $post, $product; 

    if(has_term(array(531), 'product_cat', $post->ID)) { 

     if (current_user_can('manage_options')) { 
      ?> 
      <p> 
      &darr; Download Image<br /> 
      <a href="<?php $image_id = get_post_thumbnail_id(); 
      $image_url = wp_get_attachment_image_src($image_id,'web', true); 
      echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url')?>/img/downloadForWeb.png" alt="download for web" /></a><br /> 

      <a href="<?php $image_id = get_post_thumbnail_id(); 
      $image_url = wp_get_attachment_image_src($image_id,'print', true); 
      echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForPrint.png" alt="download for print" /></a><br /> 

      <a href="<?php $image_id = get_post_thumbnail_id(); 
      $image_url = wp_get_attachment_image_src($image_id,'', true); 
      echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br /> 
      </p> 
      <?php 
     } else { 
      ?> 
      <h2>Special permission required</h2> 
      <p>In order to use this image you need special permission from the admin, please fill in the form below and we'll get back to 
      you as soon as possible...</p> 
      <?php echo do_shortcode('[contact-form 11 "special permission"]') ?> 
      <?php 
     } 
    } else { 
     ?> 
     <p> 
     &darr; Download Image<br /> 
     <a href="<?php $image_id = get_post_thumbnail_id(); 
     $image_url = wp_get_attachment_image_src($image_id,'web', true); 
     echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url')?>/img/downloadForWeb.png" alt="download for web" /></a><br /> 

     <a href="<?php $image_id = get_post_thumbnail_id(); 
     $image_url = wp_get_attachment_image_src($image_id,'print', true); 
     echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForPrint.png" alt="download for print" /></a><br /> 

     <a href="<?php $image_id = get_post_thumbnail_id(); 
     $image_url = wp_get_attachment_image_src($image_id,'', true); 
     echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url')?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br /> 
     </p> 
     <?php 
    } 
} 

有限公司de進入你活動的兒童主題(或主題)的function.php文件或者任何插件文件中。

測試和工程...

相關問題