- This topic has 14 replies, 2 voices, and was last updated 1 year, 5 months ago by
Tom.
-
AuthorPosts
-
September 1, 2019 at 9:31 am #998546
Alexander
Hello,
on my photography site I heavily rely on attachments (obviously), and I also include media in search results. Since media doesn’t have a featured image, the photos show up in the search results as text only, which is a bit of a hindrance for visual content. π
Is there a way to include the photo itself in the search results, just as if it were a featured image for a post?
Here’s what my search result for “Buckwheat” looks like right now: https://www.alex-kunz.com/?s=buckwheat – the first page is all attachments in this case, and it’s all text.
Thanks a lot
Alexander.September 4, 2019 at 3:48 pm #1001259Alexander
*ping*
(I guess I shouldn’t have started this topic on the holiday weekend?;-)
September 4, 2019 at 5:17 pm #1001287Tom
Lead DeveloperLead DeveloperHi there,
Sorry about that! Not sure how this slipped through.
Tough one! I wonder if this would do it?:
add_filter( 'generate_show_excerpt', function( $show ) { if ( is_search() && is_attachment() ) { $show = false; } return $show; } );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 4, 2019 at 8:22 pm #1001343Alexander
Hello Tom,
no worries, no hurry – thanks for your reply!
Your filter didn’t work but it made me look at the docs for
generate_show_excerpt
(I’m linking it here in case someone else needs this), and I tried the example snippet from that document to show the full post in search results – and that works.Best of all, it still shows my excerpts, since I have the “Read More” tag in all of my normal posts – or am I missing something here? Here’s what it looks like now: https://www.alex-kunz.com/page/2/?s=buckwheat (this result page contains both posts and attachments)
This leads to a follow-up question now: clicking on a search result image for an attachment opens the image file itself (because it’s the attachment page, I get it). Clicking on the image for a post in the search results open the post though (because it’s a Featured Image).
Is it possible that the images for attachments in search results (and only there) link to the attachment page instead?
Thanks
Alex.September 5, 2019 at 9:27 am #1001913Tom
Lead DeveloperLead DeveloperPerfect!
Let’s see if this works:
add_filter( 'attachment_link', function( $link, $post_id ) { if ( is_search() ) { return get_permalink( $post_id ); } return $link; }, 10, 2 );
Let me know π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 5, 2019 at 9:36 am #1001917Alexander
Thank you, Tom – unfortunately, that replaced the image with the link (I’m leaving it active so you can take a look), ie. the image itself doesn’t show anymore. Whoops!
September 5, 2019 at 9:44 am #1001922Tom
Lead DeveloperLead DeveloperJust made a change to the code above – can you give it another shot?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 5, 2019 at 9:53 am #1001928Alexander
Ooops, that resulted in a 500 Internal Server Error. :-} (I reverted it to the previous code)
Is there an error log that would help you?
September 5, 2019 at 4:24 pm #1002168Tom
Lead DeveloperLead DeveloperThat’s strange – what’s your most recent
error_log
entry say? The PHP syntax looks good.I just tweaked it a little, but I’m not seeing why it would have errored on us: https://generatepress.com/forums/topic/how-to-show-images-attachments-in-search-results/#post-1001913
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 5, 2019 at 5:01 pm #1002186Alexander
Hello Tom, I updated the snippet with your latest tweak, it doesn’t make a difference. I deactivated the Relevanssi search plugin that I’m using, no difference either.
Oddly, the error_log doesn’t contain anything useful – there’s one lonely entry in there, but it’s not from my IP, or the server’s IP.
The PHP error log’s last entry is from two days ago.
Not sure what to do. If possible, I’d like to solve this riddle, but it isn’t high on my priority list. The fact that the photos do show up in the search results is the improvement that I really wanted.
Thank you
Alex.September 6, 2019 at 9:42 am #1002749Tom
Lead DeveloperLead DeveloperTo debug, does this output “hi” on the page?:
add_filter( 'attachment_link', function( $link, $post_id ) { if ( is_search() ) { var_dump( 'hi' ); return get_permalink( $post_id ); } return $link; }, 10, 2 );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 8, 2019 at 1:03 pm #1004271Alexander
Hello Tom, sorry for the late reply, I’ve been out of town.
That doesn’t make a difference. Nothing is output. The “500 Internal Server Error” is shown without any search results appearing first (ie. I search for “buckwheat” or whatever, and after a second or so, the 500 internal server error is shown).
Thanks
Alex.September 8, 2019 at 4:29 pm #1004354Tom
Lead DeveloperLead DeveloperHmm, ok.
I have no idea if this will work, but let’s try:
add_filter( 'wp_get_attachment_link', function( $output, $id, $size, $permalink, $icon, $text, $attr ) { if ( ! is_search() ) { return $output; } $_post = get_post( $id ); $image = wp_get_attachment_image( $_post->ID, 'large', $icon, $attr ); return "<a href='" . get_permalink( $_post->ID ) . "'>$image</a>"; }, 10, 7 );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 19, 2019 at 10:55 am #1013860Alexander
Hello Tom,
a very late reply to this, I’m sorry! (I never received an email notification to it and thought we’d just leave it alone after my last reply:-) I saw your response only now when I posted a different question and looked at my forum profile.
And what can I say – this last one works! The images for attachments in search pages now link to the attachment pages. Images in posts still link to the post. Fabulous!
Thanks again & sorry that I didn’t see this any sooner.
Alex.September 19, 2019 at 6:35 pm #1014077Tom
Lead DeveloperLead DeveloperNo problem! Glad I could help π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-development -
AuthorPosts
- You must be logged in to reply to this topic.