I created a custom archive for search results.
To make it work with Relevanssi I followed this topic: https://toolset.com/forums/topic/custom-search-relevanssi/.
I added some code to functions.php of my theme. This makes highlight search term work.
You can view the result I have so far here: hidden link
First question: The excerpt in the search results is wrapped in <p></p> tags. However, I did not add these to my template in the Views WordPress archive. (view screenshot)
How can I get rid of these tags?
A second question is that I would like to show the Post type in the search results:
Filed under: Posts
Instead of "Posts" (the content type name) I would like to display: "News". How can I do that?
First question: The excerpt in the search results is wrapped in <p></p> tags. However, I did not add these to my template in the Views WordPress archive. (view screenshot) How can I get rid of these tags?
Hi, as a test, can you please temporarily remove the highlight_keyword shortcode? Is the extra p tag still included?
Instead of "Posts" (the content type name) I would like to display: "News". How can I do that?
You can use conditional HTML here. If the post type is equal to "post", then display "News" instead.
Filed under: [wpv-conditional if="( '[wpv-post-type]' eq 'post' )"]News[/wpv-conditional]
More information about conditional HTML:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
Thank you, Christian,
The conditional HTML works fine. This part is resolved.
Not yet resolved:
I temporarily removed the highlight_keyword shortcode. Then extra p tag disappeared?
How can I resolve this?
I'm not familiar with the highlight_keyword shortcode. It's not native to WordPress, so it must be coming from somewhere else, like a theme or 3rd-party plugin. Can you tell me more about how it's implemented?
Hello Cristian,
The highlight_keyword shortcode is not native to WordPress. Here is how I implemented it:
I followed this tutorial. https://toolset.com/forums/topic/custom-search-relevanssi/.
I added the following to my functions.php:
// highlight keyword with relevanssi
// https://toolset.com/forums/topic/custom-search-relevanssi/
function highlight_keyword_func($atts, $content) {
$content = do_shortcode($content);
if(isset($_GET['s'])){
$content = relevanssi_highlight_terms($content, $_GET['s']);
}
return $content;
}
add_shortcode( 'highlight-keyword', 'highlight_keyword_func' );
This is how I implemented the shortcode in the Toolset Views WordPress template:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<ul class="wpv-loop js-wpv-loop">
<wpv-loop>
<li>
<h3>[wpv-post-link]</h3>
<p><em>[wpv-post-date]<br>Filed under: [wpv-conditional if="( '[wpv-post-type]' eq 'post' )"]News[/wpv-conditional][wpv-conditional if="( '[wpv-post-type]' ne 'post' )"][wpv-post-type show="plural"][/wpv-conditional] [wpv-conditional if="( '[wpv-post-type]' eq 'post' )"] - [wpv-post-taxonomy type="category" format="name"][/wpv-conditional]</em>
<br />
[highlight-keyword][wpv-post-excerpt][/highlight-keyword]
</p>
</li>
</wpv-loop>
</ul>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-layout-end]
Okay there's an option for the wpv-post-excerpt shortcode that might help suppress these paragraph tags. Try this:
[highlight-keyword][wpv-post-excerpt format="noautop"][/highlight-keyword]
Thank you Christian, this works!