Use The Image In A Blog Post In Search Results

  • Hi Hussein,


    Thanks for your reply. It probably is best to use the perch_blog_custom(), but how do I include the search results title and number of search and the pagination using this method?


    Thanks,

  • You can use the same pagination tags you had in your previous comment, but you'll need to use the perch:blog namespace instead of perch:search for your template tags.


    As for the total number, when you enable pagination, Perch makes a number of pagination variables available to you in the template including total:


  • Sorry, this has confused me. I've tried replacing search with blog and it's not worked.


    This is my output search results page:


    This is blog_search_results.php


    PHP
    1. <?php
    2. perch_blog_custom([
    3. 'template' => 'post_search_result',
    4. 'filter' => 'postURL',
    5. 'value' => perch_layout_var('url', true),
    6. ]);
    7. ?>


    and this search-result.html


    Any help much appreciated.

  • If perch_blog_custom() is the suitable option for your use-case, you do not need to use the layout blog_search_results.php as it will make redundant database queries.


    The reason the layout is used in the original solution on this thread is because perch_content_search() does not make all the fields of a blog post available. On the other hand perch_blog_custom() does.



    The following renders the result using the template perch/templates/blog/post_search_result.html:


    Your post_search_result.html template can include all the tags you need:

  • Thanks, that's perfect!!


    How do I expand the range of filtering? Is there a list of options available (ie postTitle, postDescHTML)


    I'd like to include the main content, tags and categories.


    Also is there a way to include similar matches? As when I search tree, only items with 'tree' appear, the items with 'trees' do not.


    Thanks

  • Also is there a way to include similar matches? As when I search tree, only items with 'tree' appear, the items with 'trees' do not.


    The example I used above uses the contains filter. It performs a simple search in which it looks for exact matches.


    If you need something a little more sophisticated and you are comfortable with regular expressions, you can use the regex filter. For instance, the regex (?i).*(tree).* would match any word containing the string tree (e.g. trees, street). So this may not always return relevant results.



    If the goal is to just include plurals, one option could be to search for both the entered term and the (possible) plural form with a regex \btree\b|\btrees\b:



    Is there a way to include filtering for tags and categories?


    The documented method for tag and category filtering is to use the tag and category options:

    PHP
    1. perch_blog_custom([
    2. 'category' => ['category-path'],
    3. 'tag' => ['news'],
    4. ]);


    However, I don't think the above would be a good fit for your use case as it will only return posts that match the category/tag filters as opposed to making it optional.



    Instead you should be able to filter by postTags and _category like the other fields:

  • Hi hus_hmd , I'd just like to thank you for the instructions above :thumbup:. They worked perfectly for my search results page.


    Gareth S I do think that it's strange the image isn't included by default. It's a very common use-case to include a thumbnail image when returning a search on a blog. Perhaps something to add for the next update to the Blog Add-on? :)