I have a multisite with one of the sites being a product catalog. In the ‘main site’ I have a setup with ACF with flexible content blocks. One of the blocks is a block which shows products (from another site in the network).
I have the post ids for the posts I want to retrieve from the product catalog so I get all ACF fields/post info with a function.
function get_product_info( $ product_id ) { if ( false != $ product_id ) { switch_to_blog( 11 ); $ fields = get_field_objects( $ product_id ); restore_current_blog(); return $ fields; } return false; }
This gives me (almost) all info I need to output product info. The image which is used is stored in one of these fields which are returned.
I need a custom function to retrieve the (image) info since the info is otherwise retrieved from an image within the current site, instead of the product catalog. So I return the file location and then TimberImage that again to get a proper object. That seems to work but then size is ignored.
function get_product_image( $ image_id ) { if ( false != $ image_id ) { switch_to_blog( 11 ); $ image = new Timber\Image($ image_id); restore_current_blog(); return $ image->guid; } return false; }
Same goes for the permalink
function get_product_link( $ product_id ) { if ( false != $ product_id ) { switch_to_blog( 11 ); $ post = get_post( $ product_id ); $ link = get_permalink( $ post->ID ); restore_current_blog(); return $ link; } return false; }
I use a macro for the images, which is defined as this.
{% macro image( image, size, fallback, alt, title, class ) %} {% set fallback = fallback|default(0) %} {% if image and image.src %} <img src="{{ image.src(size) }}" alt="{{ image.alt }}" title="" /> {% elseif fallback %} <img src="{{ fallback }}" alt="" title="" /> {% endif %} {% endmacro %}
So my Twig file to output a product is as follows.
{% set product_info = function( 'get_product_info', post.ID ) %} {% set product_image = TimberImage( function( 'get_product_image', product_info.catalogue_product_image.value.ID ) ) %} <div class="product-item"> <header> {% if product_image %} <figure class="media product__image"> <a href="{{ function( 'get_product_link', post.ID ) }}"> {{ macro.image( product_image, 'thumbnail', options.images.fallback_product_teaser, alt, title, class) }} </a> </figure> {% endif %} </header> <div class="product-item__content"> <h2 class="product-item__title"> <a href="{{ function( 'get_product_link', post.ID ) }}"> {{ post.title }} </a> </h2> {# more code which is not relevant #} </div> </div>
I have a few issues which puzzle me:
- I can’t seem to get a proper Timber object which allows me to properly echo its values like
link
,title
, etc. - if I don’t use TimberImage twice I don’t seem to get any image but the sizing of an image doesn’t work. It always shows full size.
- the permalink seems to ‘ignore’ the post type. The permalink of a products should be
domain.com/product/product-slug
but returnsdomain.com/product-slug
. If I retrieve the permalink from within the product catalog, it does give me the correct link.
Note for a mod: plz add the tag timber-image
.