2017-08-17 73 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文件或者任何插件文件中。

测试和工程...

相关问题