Related field raw data problem
-
Hi!
I am trying to display relational data on a page, however I have some flaws.
First I used pods templates:
[if complementary_products] <span>Pieces of collection</span> <ul class="products column-2"> [each complementary_products] <li> <a href="{@permalink,esc_url}"> <span><img src={@post_thumbnail_url,esc_url}></span> <span>{@post_title,esc_attr}</span> <span>{@artist}</span> <span>{@_price}</span> </a> <div class="et_pb_button_module_wrapper"> <a href="add-to-cart={@id,esc_attr}" data-quantity="1" class="et_pb_button button add_to_cart_button ajax_add_to_cart" data-product_id="{@id,esc_attr}">Add to basket</a> </div></li> [/each] </ul> [/if]
This works fine, except the price value, that displays as a raw number. For this I though to replace {@_price} to: <?php wc_price(pods_field (_price))?> however I realized php is deprecated in pods templates. Therefore I created a shortcode to use as follows:
function pieces_of_collection () { $pod = pods( 'product', get_the_id() ); $related = $pod->field( 'complementary_products' ); if ( ! empty( $related ) ) { $wrap_start = ('<span>Pieces of the collection</span><ul class="products column-2">'); foreach ( $related as $rel ) { $id = $rel[ 'ID' ]; $permalink = get_permalink( $id ); $img_url = pods_field_display ('post_thumbnail_url', $id); $title = get_the_title( $id, 'post_title', true ); $artist_name = pods_field_display ('artist', $id); $price = get_post_meta( $id, '_price', true ); $price_formatted = wc_price($price); $pieces_of_collection .= ('<li><a href="'.esc_url($permalink).'"><span><img src='.$img_url.'></span><span>'.$title.'</span> <span>'.$artist_name.'</span><span class=><span>'.$price_formatted.'</span></span></a><div><a href="add-to-cart='.$id.'" data-quantity="1" class="et_pb_button button add_to_cart_button ajax_add_to_cart" data-product_id="'.$id.'">add to basket</a> </div></li>'); } $wrap_end = ('</ul>'); return $wrap_start . $pieces_of_collection . $wrap_end; } } add_shortcode( 'func_pieces_of_collection', 'pieces_of_collection' );
Now the price shows great however, pods_field_display function does not work in the loop. It repeats itself. Get_post_meta function works great but it returns unformatted raw data. Can I get some help with this?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Related field raw data problem’ is closed to new replies.