howdytom Posted October 9, 2019 Posted October 9, 2019 Hi there, sorry for asking such a dump newbie question. I am trying to create a really simple minimal blog. How I can load image(s) from a template blog-post in my home.php? I am able to print fields e.g. titles, body, however images won't show up. Also image scaling doesn't work. The Field: images is set to "array if items". I really appreciate any reply. <!-- /.Load image from blog-blog template --> <?php $blogposts = $pages->find("template=blog-post"); foreach($blogposts as $blogpost){ $blogimages = $blogpost->images; $imgthumb = $blogimages->size(500, 300); echo "<img src='{$imgthumb->url}' alt='{$imgthumb->description}'>"; echo "<h2><a href='{$blogpost->url}' >{$blogpost->title}</a></h2>"; echo "<p>{$blogpost->body}</p>"; echo "<br>"; } ?>
elabx Posted October 9, 2019 Posted October 9, 2019 I think the main issue here is like you say, they images field is an array, so you have to grab the image you need (for example, the first one): $blogimages = $blogpost->images; $imgthumb = $blogimages->first()->size(500, 300); Double check your images field is actually showing up as an array. Best of luck! 1
howdytom Posted October 9, 2019 Author Posted October 9, 2019 Thanks for your super-fast reply. Indeed, adding first() will show the blogpost image. This is exactly I was looking for. Perfect! 1
howdytom Posted December 16, 2019 Author Posted December 16, 2019 I getting back to my original question. I would love to extend my blog. I am able to display images without $pages->find() method. But how can I show ALL images in Pages::find() method? Removing first() throws an error Method Pageimages::size does not exist or is not callable in this context
gebeer Posted December 16, 2019 Posted December 16, 2019 This is basic PHP looping over an array. Since $blogimages is an array, you need to foreach over it to output every image in that array <?php $blogposts = $pages->find("template=blog-post"); foreach($blogposts as $blogpost){ $blogimages = $blogpost->images; foreach($blogimages as $image){ $imgthumb = $image->size(500, 300); echo "<img src='{$imgthumb->url}' alt='{$imgthumb->description}'>"; } echo "<h2><a href='{$blogpost->url}' >{$blogpost->title}</a></h2>"; echo "<p>{$blogpost->body}</p>"; echo "<br>"; } ?> 2 1
howdytom Posted December 16, 2019 Author Posted December 16, 2019 gebeer, thanks for your reply and answering such a basic question :-) Yes, this is exactly I was trying to achieve. I really like the Processwire forum. It is such a friendly community with helpful replies. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now