我正在使用woocommerce .我正在循环进入产品列表。
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'sections', 'order' => 'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
the_field('html_content');
?>
<input type="hidden" name="price_section" value="<?php echo$saleprice = get_post_meta( get_the_ID(), '_sale_price', true); ?>" />
<img src="<?php echo $image[0]; ?>" />
<p><?php echo the_title(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
在上面的代码中the_field('html_content');是我从自定义字段中获得的内容,它的值是这样的
<div class="dvThumb " data-section="One">
<div id="asd" name="" class="dvSectionCol" style="width:90.2px
!important; text-align:center;" >
</div>
现在在循环中,我想添加一个数据属性到html_content (div与id为asd)。请告诉我如何在循环中为每个html_content添加数据属性。
我想在这个数据属性是价格。我已经尝试了什么是你可以看到在循环中,我做了一个隐藏的输入类型,但我没有看到任何方式有一个唯一的id和名称为每个价格。那么,有没有什么方法可以为每个html_content in循环本身添加一个data属性呢?
如果我能给price添加一个数据属性那就太好了,因为这样我以后使用起来就很方便了
好了,我有办法了。我添加了一个自定义字段,存储div的ID (asd在我的情况下),并在循环本身,我使用该自定义字段存储ID分配数据属性到我的html_content;)
<script>
var for_id='<?php echo$for_id; ?>';
$("#"+for_id).attr('data-price',<?php echo$saleprice =
get_post_meta( get_the_ID(), '_sale_price', true); ?>);
</script>