Jump to content

Call 3 fields from child to parent page - stuck


Franko
 Share

Recommended Posts

I'm just trying to call 3 fields from a child page and list them on the parent page for a products list.

Here is the code. I can get the title and product description to pull but not the image. I also would like set the hight and width but every time I uncomment that it creates an error. 

foreach($page->children as $product) {
 
    if($product->image) {
       // $image = $product->image->width(150);
        $img = "<img src='{$image->url}' alt='{$product->title}' class='flt-left-img' />";
    } 
    
    else {
     
        $img = "<span class='image_placeholder'>Image not available</span>";
    } 
 
    echo "
        $img
        <h3>{$product->title}</h3>
        <p>{$product->product_description}</p>
        <hr />
        ";
}
?>
 
Advise would be appreciated. 
Link to comment
Share on other sites

Depends what field "image" is really. If it's single image field your code would be fine.

If it allows more than 1 image it will be a WireArray, and your code would fail. Then you'd need to treat it as such using

if($product->image->count){ ... }

Then you'd access images in the array using a foreach loop, or with one of the array functions $product->image->first()->url or $product->image->eq(n)->url

If that's the case you might want to set "image" to allow only 1 image (as the name suggests) ,then your code would work and be consistent with the naming. 


After all I could have also linked to the other 1'000'000 threads about this very same image field problem everybody has. :P 


But maybe it's just enough to link to the documentation which explains this again. https://processwire.com/api/fieldtypes/images/ :D

  • Like 3
Link to comment
Share on other sites

It was just the setting for the image. Thank you. It's works when I add this code to the template file but will not work if I use Hanna Code. I cannot get hanna code to output php from body content. I do have it set in body setting but still wont work. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...