Adding a data attribute to a html content (html content getting added in product looping from Custom Fields)

I am using woocommerce .I am looping into products to list them.

<?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(); ?>

In above code the_field(‘html_content’); is the content i am getting from custom field and its value is like this.

Read More
<div class="dvThumb " data-section="One"> 
 <div id="asd" name="" class="dvSectionCol" style="width:90.2px 
!important; text-align:center;" >
</div>

Now in looping I want to adda data attribute to the html_content (div with id as asd). please tell me how is it possible to add a data attribute to each html_content recieved in loop.

What I want in that data attribute is price .What I have tried is what you can see in loop that I made a hidden input type but I dont see any way to have a unique id and name for each price. So is there any way I can add a data attribute to each html_content in loop itself.

It would be great if I can add a data attribute for price because than it will be very easy for me for further use.

Related posts

1 comment

  1. Ok I got the solution ..I added one more custom field which stores the ID of the div (asd in my case) and in loop itself i use that custom field storing ID to assign a data attribute to my 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>
    

Comments are closed.