[英]Display video instead of product thumbnail on WooCommerce archive pages
I'm trying to display video of a WooCommerce product on shop page using Advanced Custom Fields plugin.我正在尝试使用高级自定义字段插件在商店页面上显示 WooCommerce 产品的视频。
I want to create a condition, if a product has a video, then show it instead of woocommerce_template_loop_product_thumbnail
.我想创建一个条件,如果产品有视频,则显示它而不是
woocommerce_template_loop_product_thumbnail
。
$file = get_field('archive_video'); if( $file ) { ?>
<video width="200" muted loop autoplay src="<?php echo $file['url']; ?>"></video> <?php
} else {
/**
* @hooked woocommerce_template_loop_product_thumbnail - 10;
*/
}
Am I choosing the correct approach?我选择了正确的方法吗?
Here how it should look like:这里应该是这样的:
Try the following way using hooks instead of overriding template:使用钩子而不是覆盖模板尝试以下方式:
add_action( 'woocommerce_before_shop_loop_item_title', 'action_template_loop_product_thumbnail', 9 );
function action_template_loop_product_thumbnail() {
global $product;
$file = get_field('archive_video', $product->get_id());
if( isset($file['url']) && ! empty($file['url']) ) {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '<video width="200" muted loop autoplay src="' . $file['url'] . '"></video>';
}
}
Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 It should works.
它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.