Jump to content

Is this a proper way to get an image from the Home page?


MarcC
 Share

Recommended Posts

I'm seeing this error in the logs:

2012-01-18 09:23:37: guest:/pagename/:Error:Call to a member function width() on a non-object (line 57 of /home/blah/public_html/site/templates/sidebar.inc.php)

Here's the code in question:

(56) $newsletter = $pages->get('/');
(57)                $newsThumb = $newsletter->sidebar_image->width(155);

The sidebar seems to render OK, there's an image in the field in question, and it is being resized properly. But I fear my way of getting the page or image is a bit off. Thanks :)

Link to comment
Share on other sites

  • 6 months later...

Hi there,

don't know if there's already a solution. I was trying the following:

$image = $blog_entry->blog_images->first();
$blog_thumb = $image->width(500);

As result I get:

Error Call to a member function width() on a non-object

As I found out, the $image object seems to be returned propably.

Any ideas, what's going on?

Thanks for your replies in advance!!

Link to comment
Share on other sites

@jan Are you using the new blog profile or did you set the value of $blog_entry in your own code in this template?

Here is an example using a normal wire $page array

<?php

// get the image $page if your talking normal pw. If your using the blog profile then ..
$image = $page->blog_images->first();
// check to see if you have an image
if ($image) {
// set the image size (crop to 500x500)
$blog_thumb = $image->width('500');
// output your image
echo "<img src='{$blog_thumb}>";
}
Link to comment
Share on other sites

@JeffS Thanks a lot for your replay. Im not using the new blog profile. Here's some more code. I'm using something similar on another page and everthing works fine. I can't see a difference. Can you find any mistakes?!

$blog_entries = $pages->get("id=1008")->children;
foreach($blog_entries as $blog_entry) {
	$image = $blog_entry->blog_images->first();
	$blog_thumb = $image->width('500');
	echo "<div class='blog_overview_item' >";
		echo "<h2><a href=\"{$blog_entry->url}\">{$blog_entry->title}</a></h2>";
		echo "<p>posted on ". date("d.m.Y, H:i", $blog_entry->created) ."</p>";
		echo '<div class="blog_preview_image">';
			echo "<a href='{$blog_entry->url}'><img width='{$blog_thumb->width}' height='{$blog_thumb->height}' src='{$blog_thumb->url}' alt='{$blog_thumb->description}' /></a>";
		echo "</div>";
	echo "</div>";  
}

If I remove resizing of the image, everything works fine. If I try $image->width('500'); or $image->size(500, 500); I get the error.

Any ideas?

Link to comment
Share on other sites

Remove the single quotes around the 500 maybe? It's expecting an integer and in quotes you're passing it as a string.

Oh, then I read the next bit where you did it without quotes and it was no better...

Link to comment
Share on other sites

@JeffS Thanks a lot for your replay. Im not using the new blog profile. Here's some more code. I'm using something similar on another page and everthing works fine. I can't see a difference. Can you find any mistakes?!

$blog_entries = $pages->get("id=1008")->children;
foreach($blog_entries as $blog_entry) {
	$image = $blog_entry->blog_images->first();
	$blog_thumb = $image->width('500');
	echo "<div class='blog_overview_item' >";
		echo "<h2><a href=\"{$blog_entry->url}\">{$blog_entry->title}</a></h2>";
		echo "<p>posted on ". date("d.m.Y, H:i", $blog_entry->created) ."</p>";
		echo '<div class="blog_preview_image">';
			echo "<a href='{$blog_entry->url}'><img width='{$blog_thumb->width}' height='{$blog_thumb->height}' src='{$blog_thumb->url}' alt='{$blog_thumb->description}' /></a>";
		echo "</div>";
	echo "</div>";  
}

If I remove resizing of the image, everything works fine. If I try $image->width('500'); or $image->size(500, 500); I get the error.

Any ideas?

Any more informations? Does it fail on all or just on some. What line is the error?

You could try this modified code:

<?php

$blog_entries = $pages->get(1008)->children;
foreach($blog_entries as $blog_entry) {
   $blog_thumb = false;
   if(count($blog_entry->blog_images)){
       if($blog_entry->blog_images->first()) {
           $image = $blog_entry->blog_images->first();
       } else $image = $blog_entry->blog_images;
       $blog_thumb = $image->width(500);
   }
   echo "<div class='blog_overview_item' >";
       echo "<h2><a href=\"{$blog_entry->url}\">{$blog_entry->title}</a></h2>";
       echo "<p>posted on ". date("d.m.Y, H:i", $blog_entry->created) ."</p>";
       echo '<div class="blog_preview_image">';
       if($blog_thumb) {
           echo "<a href='{$blog_entry->url}'><img width='{$blog_thumb->width}' height='{$blog_thumb->height}' src='{$blog_thumb->url}' alt='{$blog_thumb->description}' /></a>";
       }
       echo "</div>";
   echo "</div>";  
}
Link to comment
Share on other sites

Oh sh**. I'm sorry. I found my mistake and it's really more than simple!

I made two new entries in Processwire backend to test the blog. One of these two entries had images, the other didn't have any...

So thanks @Soma you were on the right track! I just tried to change size of an image that was not there. Surprise!

Ok after all, thanks for your effort!!! *embarrassing*

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...