New: Learn Statamic on Laracasts!

We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Give collection a specific url for search results

Kelly July 15, 2019 by Kelly

On my site I have a collection called resources.

This collection has 3 fields: 1) resource_url 2) title 3) thumbnail

I'm using this collection as a way to load links to relevant blog posts on a different url. So, let's say on my site's page I have a page about dog walking, I'm also loading the dog relevant blog posts from this resources collection to display with a thumbnail image on the Dog Walking page. The url is a click to the external blog post.

So, on my site's search, I DO want the resources collection to be indexed. I've successfully figured out how to make that collection's search results look different than the others. This code works in showing just the results with the resource_url from resources.

{{ search:results collection:is="resources" }}
        <a href="{{ resource_url }}"><h3>{{ title }}</h3>
        <code>{{ resource_url }}</code></a>
        {{ /search:results }}

What I don't know how to do is to combine this collections results with the general search results. So the results would first say, No Results if nothing is found. Then it would list all the search results and if it was a result in the collection:resources it would display a different url.

Perhaps something like this, (but it doesn't work)

<article class="content">
    {{ search:results }}
        {{ if no_results }}
            No results.
        {{ /if }}
                    {{ if search:results collection:is="resources" }}
        <a href="{{ resource_url }}"><h3>{{ title }}</h3>
        <code>{{ resource_url }}</code></a>
        {{ /if }}
        <div class="block mt1">
            <a href="{{ url }}">
                <h3>{{ title }}</h3>
                <p>{{ content strip_tags="img|a|p" safe_truncate="180|..." }}</p><code>{{ url }}</code></a></div>{{ /search:results }}</article>

My syntax and understanding of how to do ifs within search:results needs help. Thank you!

Answered by Jack McDade!
>>>>>>> Answered <<<<<<<
2 Replies
2 Followers