Hi, I have a blog page that is filtered by topic. Is there a way to add a search bar next to the topic filter that searches the blog listing for keywords and then displays the results in the same card format? Here is the live page: Channel Marketing Blog | 360insights
Topic filter code:
<div class="blog-listing-bar__topics">
<form>
<label for="topics">{% text "browse_by_topic_title" label="Browse by Topic Title", value="Browse by topic" %}</label>
<select name="topics" id="topics" class="mb-none">
{% set topics = blog_topics('default', 250) %}
<option value="{{ group.absolute_url }}">All Topics</option>
{% for topic in topics %}
<option value="{{ group.absolute_url }}/tag/{{topic.slug}}" {% if topic.slug == tag %}selected{% endif %}>{{ topic }}</option>
{% endfor %}
</select>
</form>
</div>
Card display code:
<div class="cards cards--3">
{% for content in contents %}
{% if content.widgets.resource_url.body.value %}
{% set postUrl = content.widgets.resource_url.body.value %}
{% else %}
{% set postUrl = content.absolute_url %}
{% endif %}
{% if not simple_list_page %}
<a href="{{ postUrl }}" class="card {% for topic in content.topic_list %} {{ topic.slug }}{% if not loop.last %}{% endif %}{% endfor %}" data-topics="{% for topic in content.topic_list %}{{ topic.slug }}{% if not loop.last %}, {% endif %}{% endfor %}">
<div class="card__image">
<img src="{{ content.featured_image }}" alt="" loading="lazy">
</div>
<div class="card__details">
<div class="card__title">
{{ content.name }}
</div>
{% if card.description %}
<div class="card__desc">
{{ content.post_list_content|safe|striptags|truncatehtml(150, '...', false) }}
</div>
{% endif %}
<span class="button button--arrow button--notext"></span>
</div>
</a>
{% else %}
<h2 class="blog-listing__post-simple"><a href="{{ content.absolute_url }}">{{ content.name }}</a></h2>
{% endif %}
{% endfor %}
</div>
- Then in the template layout you'd use the "is_search_results" field I inserted above to detect that search reults are being shown and hide the regular content loop
{% if request.query_dict.is_search_results == "true" %}
[LAYOUT OF THE TEMPLATE WITH SEARCH RESULTS]
{% else %}
[NORMAL LAYOUT OF THE TEMPLATE]
{% endif %}
Note in the case you'll also need to change the url the custom search module uses for the "Previous" and "Next" links.
Nov 10, 20224:40 AM - edited Nov 10, 20224:41 AM
Top Contributor
Add a Keyword Filter / Search
SOLVE
Hi Barry,
If I select a template to use in https://app.hubspot.com/settings/[YOUR-PORTAL-ID-GOES-HERE]/website/pages/all-domains/system-pages this would change any global search that we add in the furture. We want the main global search to display results as a header and first couple of line of any content that matches keywords. I want this blog search to only bring back matching blogs in the same visual card style. If I tailor the search page to display the blog results as cards by calling the blog.css and use that template here https://app.hubspot.com/settings/[YOUR-PORTAL-ID-GOES-HERE]/website/pages/all-domains/system-pages it will try and display everything that matches the keywords as a card. How would I stop the global search when we implement it from using the blog.css blog card style?
- Then in the template layout you'd use the "is_search_results" field I inserted above to detect that search reults are being shown and hide the regular content loop
{% if request.query_dict.is_search_results == "true" %}
[LAYOUT OF THE TEMPLATE WITH SEARCH RESULTS]
{% else %}
[NORMAL LAYOUT OF THE TEMPLATE]
{% endif %}
Note in the case you'll also need to change the url the custom search module uses for the "Previous" and "Next" links.
I would like the results to populate where the current blog listing is, similar to how the current topic filter reloads the same page with the filtered blogs. In this instance it would reload the same page but with only the keyword relevent blogs appearing.
If I use my code above it loads a the results into a separte page due to this code
What you'd need to do is edit/create a new search results template that includes the same modules as the blog-list template (e.g. hero, blog-listing-bar, etc.)
So I am effectivly creating another page with exactly the same framework and content but with the search results module replacing where the current blog listing is? The standard results list will then just pull in the blog header and first couple of lines of the blog. I would then need to get the results listing to pull the thumbnail and style the list as cards.
Hi Barry, is there a way I can create a staging or test area for this blog listing page? I would need to check that the code is working so that I can test the listing page is pulling the results and displaying them correctly. Thanks