Jump to content
howdytom

Newbie question: How I can load image(s) from another template blog-post

Recommended Posts

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>";
      }
  	?>

 

Share this post


Link to post
Share on other sites

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!

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks for your super-fast reply. Indeed, adding first() will show the blogpost image. This is exactly I was looking for. Perfect!

  • Like 1

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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>";
      }
  	?>

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

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.

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...