hide picture caption in results
-
At the top of a page, is a picture with a caption.
In the search results this shows up as
‘pic caption’ Now here is the real text from the page.
How can i hide / remove the pic caption from the search results excerpt ?
thanks
-
Modify your search results template.
?? how
in the results, its not recognisable as a picture caption.It does not show
<caption>pic caption</caption> and now the real text
it just shows
pic caption and now the real text– where/how are the excerpts created ?
or what should i modify ?Your theme has a search results template which determines what the search results look like.
Or do you mean that Relevanssi-generated excerpts contain the picture caption?
I mean the Relevanssi-generated excerpts contain the picture caption
Thats what i’m trying to get rid of.
The excerpt should just be
“here is the text with the search WORD in it”
and not
“PICTURE CAPTURE here is the text with the search WORD in it”
The picture capture does not contain the search word at all, and should not be in the excerpt.
Can Relevannssi not ignore, or at least not include in the excerpt, the the search term is not in it, anything between the caption tags ?
<caption>PLEASE IGNORE ME</caption>Or at least if it kept the caption tags, in the excerpt, i could hide it with css
How do those picture captions appear? Relevanssi does remove tags from excerpts, but it keeps the tag contents. I’m reluctant to just zap the caption content, because some people might want to keep it.
Anyway, I do have a filter hook you can use to get rid of this. The excerpt content is passed through
relevanssi_excerpt_content
filter before it’s processed, so write a function on that filter hook to remove the captions. Something like this:add_filter('relevanssi_excerpt_content', 'remove_captions'); function remove_captions($excerpt) { $content = preg_replace('/<caption>.*?</caption>/', '', $content; return $content; }
I haven’t tested this so it might not work, but that’s the basic idea anyway.
its getting there, you’re missing a ) behind content.
and escaping the forward slash in catptionadd_filter('relevanssi_excerpt_content', 'remove_captions'); function remove_captions($excerpt) { $content = preg_replace('/<caption>.*?<\/caption>/', '', $excerpt); return $content; }
BUT and its a big but ??
This filters the excerpt, and in the excerpt i no longer have the caption tag, just the content of the caption tag.
I need to use this, beforehand, where you actually create the excerpt, because afterwards you cannot tell, what is the caption textIn that case, change
relevanssi_excerpt_content
torelevanssi_pre_excerpt_content
.That theoretically would be perfect.
Do i need to regenerate the excerpts ?
I would presume yes
Have theoretically re-indexed, and the cache says 0
Also changed caption to figcaption as thats the tag.But no change ?
Any ideasSince this is about excerpts, there’s no need to reindex. The excerpts are generated on the fly (unless you use the cache, and I really recommend you don’t).
If nothing changes, I’d start by debugging the function. Check $content before the preg_replace() – does it contain the caption tag? Does the preg_replace() work, does it remove the caption correctly? If it doesn’t, then the preg_replace() needs to some fixing.
Ok have debugged a bit more ??
And discovered in the excerpt it also actually the picture too, which could stay there, that get removed anyway.
So the excerpt looks like this[caption id="" align="alignnone" width="300"]<a href="uploads/2013/08/Werkprojekt_Musik-II.png"><img title="Werkprojekt Musik" alt="Werkprojekt_Musik II" src="uploads/2013/08/Werkprojekt_Musik-II-300x184.png" width="300" height="184" /></a> 'Blues Brothers' I[/caption] ? Seit dem Schuljahr 2012/13 ist für die zum 11. Schuljahr verbleibenden
So what i’m now trying to do, unfortunately my regex is rubbish is remove everything between the
</a>
the closing caption [/caption]I came up with this
$content = preg_replace("a>(.*\n*)[", "a>[", $content);
but it doesnt work, also tried
$content = preg_replace("a>.*?[", "a>[", $content);
Having now messed about with regex for a bit ??
I love regex, i’ve ended up with the following which works!$content = preg_replace("/<\/a>.*?\[/", "</a>[", $content);
Turns out though, that some captions are in a dd tag, and some are in a figcaption tag.
So my now final, it actually seems to work function is as suchadd_filter('relevanssi_pre_excerpt_content', 'remove_captions'); function remove_captions($content) { $content = preg_replace("/<\/a>.*?\[/", "</a>[", $content); $content = preg_replace("/<dd>(.*)<\/dd>/", "", $content); $content = preg_replace("/<figcaption>(.*)<\/figcaption>/", "", $content); return $content; }
Many many many thanks for your help with this
I am a beginner. Could you please tell which php files I have to debug?
go to your theme folder, and then in the root of your theme folder, there should be a file
functions.php (if not there create it)and then just add to it as i said above
add_filter('relevanssi_pre_excerpt_content', 'remove_captions'); function remove_captions($content) { #error_reporting(E_ALL); $content = preg_replace("/<\/a>.*?\[/", "</a>[", $content); $content = preg_replace("/<dd>(.*)<\/dd>/", "", $content); $content = preg_replace("/<figcaption>(.*)<\/figcaption>/", "", $content); return $content; }
and that should then be working (providing relevanssi is activated)
Thank you very much, it really works!
Hi,
in some cases when search word was at the beginning of the post picture caption was still being displayed. Adding this line to function solved problem:
$content = preg_replace(“/\[caption .+?\[\/caption\]/”, “”, $content);
source of this line of code: https://www.ads-software.com/support/topic/how-to-remeove-first-image-from-the_content
thanks to member talkstory
so function looks like this:add_filter('relevanssi_pre_excerpt_content', 'remove_captions'); function remove_captions($content) { #error_reporting(E_ALL); $content = preg_replace("/<\/a>.*?\[/", "</a>[", $content); $content = preg_replace("/<dd>(.*)<\/dd>/", "", $content); $content = preg_replace("/<figcaption>(.*)<\/figcaption>/", "", $content); $content = preg_replace("/\[caption .+?\[\/caption\]/", "", $content); return $content; }
- The topic ‘hide picture caption in results’ is closed to new replies.