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!
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!
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
Fatal error: Call to a member function getAttribute() on a non-object in /Applications/MAMP/htdocs/mysite/blocks/search/view.php on line 9
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=\"\" />"; ?>
Thanks Paulo!
This is allowing the loop to run, and not raising any errors - but unfortunately not returning any value.
This is allowing the loop to run, and not raising any errors - but unfortunately not returning any value.
ensure that $img is $page_thumb
Sorry paulo - I'm missing something in translation, this doesn't work for me.
You're suggesting this right? :
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=\"\" />"; ?>
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.
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>
Viewing 15 lines of 36 lines. View entire code block.
$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.
I can never remember which of these work in a given instance.
Try one of those and then if you just want to output it do
Hope that helps!
$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!
Thank You very much for this!
i took
i took
$page_thumb = $c->getAttribute('page_thumbnail');
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.
Anyone any ideas? I'm really banging my head against the wall on this one.
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 ...
... Had to bump this, I'm going slowly mad ...
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>
Viewing 15 lines of 37 lines. View entire code block.
This seems very handy. Thank you!
Where exactly do you place this code?
Where exactly do you place this code?
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!
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!
Thank you very much!
Works like a charm. I would absolutely write this up as a how-to...
Cheers :)
Works like a charm. I would absolutely write this up as a how-to...
Cheers :)
File does not exist.
this error is coming to me
this error is coming to me
This is how I would do it (but it's untested), but you will probably get a better answer:
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