Adding Page Thumbnail to search results

Permalink 2 users found helpful
Hello,

I'm building a custom template for the standard C5 search block results.

Does anyone know how best to return an image which is stored as a page attribute ('page_thumbnail'), against each result?

Thanks!

drbiskit
 
adajad replied on at Permalink Reply
adajad
You are halfway there.

This is how I would do it (but it's untested), but you will probably get a better answer:
<?php 
    $page_thumb = $c->getAttribute('page_thumbnail');
    $page_thumb_url = $page_thumb->getURL();
?>


After that you can use $page_thumb_url wherever you want. You can also skip the population of $page_thumb_url and just use $page_thumb->getURL();.

You need to fetch the thumbnail for each page inside your loop.

Edit: what you will get is the url for the thumbnail (duh..) which you need to use with a normal image tag like
<img src="<?php echo $page_thumb_url; ?>" width="" height="" alt="" />
drbiskit replied on at Permalink Reply
drbiskit
Thanks adajad - Yeah, this is pretty much the same as what I had been trying to implement, but something isn't happy - it returns:

Fatal error: Call to a member function getAttribute() on a non-object in /Applications/MAMP/htdocs/mysite/blocks/search/view.php on line 9
PauloCarvalhoDesign replied on at Permalink Reply
PauloCarvalhoDesign
You need to call the image helper for it to work.
<?php
$ih = Loader::helper('image');
    $thumb = $ih->getThumbnail($img, 40, 30); //<-- set these 2 numbers to max width and height of thumbnails
    echo "<img src=\"{$thumb->src}\" width=\"{$thumb->width}\" height=\"{$thumb->height}\" alt=\"\" />";
?>
drbiskit replied on at Permalink Reply
drbiskit
Thanks Paulo!

This is allowing the loop to run, and not raising any errors - but unfortunately not returning any value.
PauloCarvalhoDesign replied on at Permalink Reply
PauloCarvalhoDesign
ensure that $img is $page_thumb
drbiskit replied on at Permalink Reply
drbiskit
Sorry paulo - I'm missing something in translation, this doesn't work for me.

You're suggesting this right? :

<?php
$ih = Loader::helper('image');
    $thumb = $ih->getThumbnail($page_thumb , 40, 30); //<-- set these 2 numbers to max width and height of thumbnails
    echo "<img src=\"{$thumb->src}\" width=\"{$thumb->width}\" height=\"{$thumb->height}\" alt=\"\" />";
?>
drbiskit replied on at Permalink Reply
drbiskit
Still can't get this to work. Tried everything suggested so far in thread but with no joy... Can anyone shed any light on what I might be doing wrong?

Thanks.

<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
$textHelper = Loader::helper("text"); 
$imgHelper = Loader::helper('image'); 
?> 
<?php  if (isset($error)) { ?>
   <?php echo $error?><br/><br/>
<?php  } ?>
<?php  if( strlen($title)>0){ ?><h2>Search results for: <strong><?php echo htmlentities($query, ENT_COMPAT, APP_CHARSET)?></strong></h2><?php  } ?>
<?php  
if ($do_search) {
   if(count($results)==0){ ?>
      <h2><?php echo t('There were no results found. Please try another keyword or phrase.')?></h2>   
   <?php  }else{ ?>
         <ul>
mkly replied on at Permalink Reply
mkly
$page_thumb = $imgHelper->getThumbnail('page_thumbnail', 250, 999);

maybe this instead?
$page = Page::getCurrentPage();
$ak = CollectionAttributeKey::getByHandle('page_thumbnail');
$avo = $page->getAttributeValueObject($ak);
$page_thumb = $imgHelper->getThumbnail($avo, 250, 999);

There is a better way than that, i just can't remember it right now.
aghouseh replied on at Permalink Reply
aghouseh
I can never remember which of these work in a given instance.

$page_thumb = $c->getCollectionAttributeValue('page_thumbnail');


$page_thumb = $c->getAttribute('page_thumbnail');


Try one of those and then if you just want to output it do

echo Loader::helper('image')->outputThumbnail($page_thumb, 400, 300); // w: 400, h: 300


Hope that helps!
MathiasB replied on at Permalink Reply
MathiasB
Thank You very much for this!
i took
$page_thumb = $c->getAttribute('page_thumbnail');
drbiskit replied on at Permalink Reply
drbiskit
Thanks guys... Still no joy with this though. Just can't get it to return any value for the img src.

Anyone any ideas? I'm really banging my head against the wall on this one.
drbiskit replied on at Permalink Reply
drbiskit
Does anyone know how to return page search results showing an image (page attribute: 'page_thumbnail'), against each result? Tried all of the above but none of them work...

... Had to bump this, I'm going slowly mad ...
drbiskit replied on at Permalink Best Answer Reply
drbiskit
Worked this one out - In case anyone else needs to do this, here is how...

<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
$textHelper = Loader::helper("text");
$ih = Loader::helper('image');
?> 
<?php  if (isset($error)) { ?>
   <?php echo $error?><br/><br/>
<?php  } ?>
<?php  if( strlen($title)>0){ ?><h2>Search results for: <strong><?php echo htmlentities($query, ENT_COMPAT, APP_CHARSET)?></strong></h2><?php  } ?>
<?php  
if ($do_search) {
   if(count($results)==0){ ?>
      <h2><?php echo t('There were no results found. Please try another keyword or phrase.')?></h2>   
   <?php  }else{ ?>
         <ul>
anchoredbutterfly replied on at Permalink Reply
anchoredbutterfly
This seems very handy. Thank you!
Where exactly do you place this code?
drbiskit replied on at Permalink Reply
drbiskit
This is presuming that you already have a search results page setup and working correctly...

http://www.concrete5.org/documentation/general-topics/search/...

...and that you have already setup page thumbnails as a page attribute...

(Create a new page attribute. choose the "Image/File" type from the drop down, then call the new attribute: "Page Thumbnail" with a handle of "page_thumbnail".)

You then need to make a custom template file with this code. So paste it into a file called 'view.php' and put it here:

/blocks/search/templates/search-list-with-thumbs

Then go to your search results page (in edit mode), click on your results block, and select 'Custom template' from the popup menu. You should see your 'Search list with thumbs' as one of the options. Select it, and save. Your search list should now be returning with thumbnails showing.

Hope that helps!
anchoredbutterfly replied on at Permalink Reply
anchoredbutterfly
Thank you very much!
Works like a charm. I would absolutely write this up as a how-to...

Cheers :)
nausif replied on at Permalink Reply
File does not exist.

this error is coming to me