Jump to content

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


howdytom
 Share

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

 

Link to comment
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
Link to comment
Share on other sites

  • 2 months later...

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 

Link to comment
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
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...