Skip to content
Advertisement

PHP echo mehod ignores HTML part

I have written the following PHP code:

    function x_woocommerce_after_shop_loop_item() {
  
    $leistung = the_field('leistung_in_w');
    if ( ! empty( $leistung ) ) {
    echo '<div class="product-meta-leistung">Leistung:' . $leistung . 'W</div>';
    }
  
  echo '</div>';
}

the_field() is a function of “advanced custom fields” Plugin. https://www.advancedcustomfields.com/resources/the_field/

The problem is that only the variable is output, but not the HTML part. How can I solve the problem?

Advertisement

Answer

I don’t know why, but the following code worked:

    function x_woocommerce_after_shop_loop_item() {
    
    global $product;
    $id = $product->get_id();
    $leistung = get_post_meta($id,'leistung_in_w',true);
    if ( ! empty( $leistung ) ) {
    echo '<div class="product-meta-leistung">Leistung: ' . $leistung . 'W</div>';
    }
    
    
  echo '</div>';
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement