Jump to content

Imagefild NULL when call from function for current page.


kater
 Share

Recommended Posts

Dear Forum,

When i'm using a function for a teaser which calls a couple pages by template - including the current, just the imagefield (limit 1, automatic) for the current page returns a NULL. All other fields or pages work. When on a page that wont be repeated in the teaser, all things smooth.

Any hint what I'm doing wrong or some logic i dont get (again)?

thanks alot!

ProcessWire 2.6.1

<div class="uk-grid uk-grid-small uk-margin-top">
	<?php echo renderTeaser("template=event, teaser=1, limit=3, sort=sort", 1); ?>
</div>
function renderTeaser($template, $showdate) {

	$out='';
	$item='';
	$items='';
	$teaser='';

	$items = wire('pages')->find($template);

	if (count($items)) {
		foreach($items as $item) {
			
			//removed stuff to shorten code for forum

			if ($item->teaserimage) { 
			$thumb = $item->teaserimage->size(480,323);
			$out .= '<img src="' . $thumb->url . '" alt="' . $item->description . '">';
			}

			//removed stuff to shorten code for forum

		}
	}
	return $out;
}

Link to comment
Share on other sites

if ($item->teaserimage) { 
$thumb = $item->teaserimage->size(480,323);
$out .= '<img src="' . $thumb->url . '" alt="' . $item->teaserimage->description . '">';
}

$thumb is the size of the image not the image itself. Corrected code above.

Edit bullshit: Everything fine in your code except description. :P Too late yesterday night...

Edited by kixe
  • Like 1
Link to comment
Share on other sites

$thumb is the size of the image not the image itself. Corrected code above.

thanks for your reply!

but i'm afraid thats not the problem here.

I've tried different approaches now and no matter if i call/loop the teaser pages via function ('wire') or at the $page template itself, (just) the imagefield from the active page is always NULL when identical with the current page.

Link to comment
Share on other sites

Corrected my last post. Sorry for this.

I tried your code and everything is working like expected using single image. Please check the following:

Selectorstring teaser=1 needs field 'teaser' having int value = 1

If you use image arrays you need to use WireArray Api

// will match first image of array
if ($item->teaserimage) { 
$thumb = $item->teaserimage->first()->size(480,323);
$out .= '<img src="' . $thumb->url . '" alt="' . $thumb->description . '">';
}

// will match 4th image in array
if ($item->teaserimage) { 
$thumb = $item->teaserimage->eq(3)->size(480,323);
$out .= '<img src="' . $thumb->url . '" alt="' . $thumb->description . '">';
}

// will match last image in array
if ($item->teaserimage) { 
$thumb = $item->teaserimage->last(3)->size(480,323);
$out .= '<img src="' . $thumb->url . '" alt="' . $thumb->description . '">';
}

More about WireArrays: https://processwire.com/api/arrays/

Link to comment
Share on other sites

Selectorstring teaser=1 needs field 'teaser' having int value = 1

Hi kixe,

I should have posted the whole teaser code. The selector teaser=1 ist a checkbox to enable frontpage display for a certain (event) page. The situation also persists when i define a single item imagefield. I did not mention that the field only holds one image anyway in automatic setting. Sorry.

The teaser works on any item in the $items array "teaserimage" field but the current. All other fields in this item work as well. When i exclude the current page from the selector (id!=$page->id), everything as expected fine.

When i run the teaser function a second time, all same imagefields of the displayed pages return NULL.

It seems, i cannot get a page twice without the imagefield becoming a non-object.

function renderTeaser($template, $showdate) {

	$out='';
	$item='';
	$items='';
	$teaser='';

	$items = wire('pages')->find($template);

	if (count($items)) {
		foreach($items as $item) {
			$out .= '<div class="uk-width-1-1 uk-width-small-1-3">';

			if ($item->externallink) {
				$out .= '<a href="' . $item->externallink . '" title="' . $item->title . '">';
				} else {
				$out .= '<a href="' . $item->url . '" title="' . $item->title . '">';
				}

			$out .= '<figure class="uk-overlay">';
			
			if ($item->teaserimage) {
				$thumb = $item->teaserimage->size(480,323);
				$out .= '<img src="' . $thumb->url . '" alt="' . $item->teaserimage->description . '">';
			}

			if ($item->host){
				$logo = wire('pages')->get("$item->host")->logo->url;
				$out .= '<img class="teaserlogo" src="' . $logo . '" alt="' . $item->title . '">';
			}

			$out .= '<figcaption class="uk-overlay-panel uk-overlay-background uk-overlay-bottom">';
			$out .= '<strong>' . $item->title . '</strong><br>';
			if ($showdate==1) $out .= strftime('%e. %b.', $item->event_start) . ' – ' . strftime('%e. %b. %Y', $item->event_ende) . '<br>';
			if ($item->template=="news") $out .= $item->summary;
			$out .= '</figcaption></figure></a></div>';
		}
	}
	return $out;
}

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