Jump to content

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...