Joel Natividad Joel Natividad - 3 months ago 5
PHP Question

How to put commas in my product attributes?

I inserted the code below in my content-product.php and the result is:


1 year2 year3 year4 year


How can I put commas in my attributes and change the year to yr?

<p class="availsize"><?php
echo "Available Sizes: ";
$subheadingvalues = get_the_terms( $product->id, 'pa_available-sizes');

foreach ( $subheadingvalues as $subheadingvalue ) {
echo $subheadingvalue->name;
}
?></p>

Answer

I have used a variable $subheading to collect all data with a ,(comma) at the end. I did this because all the data will be separated by a comma along the a , at the end. Like data1,data2,data3,. Then the trim function is used to remove the , at the end.

<p class="availsize">
    <?php
        echo "Available Sizes: ";
        $subheadingvalues = get_the_terms( $product->id, 'pa_available-sizes');

        $subheading=''; //for collecting
        foreach ( $subheadingvalues as $subheadingvalue ) {
            $subheading.= $subheadingvalue->name . ',';
        }
        echo trim($subheading, ',');
    ?>
</p>
Comments