有条件地在 WooCommerce 自定义产品页面上添加自定义内容
Conditionally adding custom content on WooCommerce custom product pages
大家早上好,
我创建了一段代码,可以从 post 图片中提取相关信息并显示 3 个下载选项。
我已经将 Woocommerece 产品的网站设计更改为 运行,我如何调整我的代码片段以允许此功能工作?
<?php if(in_category(531)) { ?>
<?php if ( current_user_can('manage_options') ) { ?>
<p>
↓ 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>
↓ 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 } ?>
正确的方法似乎是使用挂钩在 woocommerce_before_add_to_cart_form
操作挂钩中的自定义函数,将您的代码嵌入其中并将其显示在您的产品页面中。
But you need to be sure that the ID 511
is a product category (and not a normal WP category. If not you will be obliged to create it and to replace this ID by the name, the slug or the ID of your new product category.
这应该是您的代码:
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>
↓ 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>
↓ 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
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……
大家早上好,
我创建了一段代码,可以从 post 图片中提取相关信息并显示 3 个下载选项。
我已经将 Woocommerece 产品的网站设计更改为 运行,我如何调整我的代码片段以允许此功能工作?
<?php if(in_category(531)) { ?>
<?php if ( current_user_can('manage_options') ) { ?>
<p>
↓ 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>
↓ 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 } ?>
正确的方法似乎是使用挂钩在 woocommerce_before_add_to_cart_form
操作挂钩中的自定义函数,将您的代码嵌入其中并将其显示在您的产品页面中。
But you need to be sure that the ID
511
is a product category (and not a normal WP category. If not you will be obliged to create it and to replace this ID by the name, the slug or the ID of your new product category.
这应该是您的代码:
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>
↓ 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>
↓ 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
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……