2

I'm having an array of different posts and products:

array (size=2)
   0 => 
      array (size=2)
         'acf_fc_layout' => string 'post'
         'linked_post' => int 6802
   1 => 
      array (size=2)
         'acf_fc_layout' => string 'product'
         'linked_product' => int 5140

My problem now is that I want the array of post/and product to contain the entire post/product object instead of just the ID. Im also using twig which makes it hard to query the object inside the view. So what I've tried is to do it from the backend side:

// Getting the array of Posts and Products
$gallerix = get_field('gallerix_layout', 'options');

// Trying to overwrite the value in the loop
foreach ($gallerix as $gallerix_item) {

    if ( $gallerix_item->acf_fc_layout == 'product' ) {

       $gallerix_item->linked_product = wc_get_product( $gallerix_item->linked_product );

    } elseif ( $gallerix_item->acf_fc_layout == 'post' ) {

       $gallerix_item->linked_post = get_post( $gallerix_item->linked_post );

    }
}

// Pass the array to Timber/Twig
$context['gallerix'] = $gallerix;

// Render twig template
Timber::render( 'views/template.twig', $context );

Hope somebody understands my problem. Any support is very appreciated.

1 Answer 1

1

I think your problem is that your are updating a temporary variable inside you foreach() loop. and your changes are not stored in your $gallerix array.

Try this :

<?php
foreach ($gallerix as $key => $gallerix_item) {
   //...
   $gallerix[$key]->linked_product = wc_get_product(...);
   //...
}
?>

instead of change $gallerix_item variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.