-
-
Save searchwpgists/2ce4efcc02d16470eaeca75d08b6b964 to your computer and use it in GitHub Desktop.
Set image thumbnail for media image results on search page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Set image thumbnail for media image results on search page | |
| add_filter( 'post_thumbnail_html', function( $html, $post_id ) { | |
| // Check if we're in a search context | |
| if ( | |
| ( | |
| is_search() | |
| || doing_action( 'wp_ajax_searchwp_live_search' ) | |
| || doing_action( 'wp_ajax_nopriv_searchwp_live_search' ) | |
| || isset( $_REQUEST['swps'] ) | |
| ) | |
| && 'attachment' === get_post_type( $post_id ) | |
| ) { | |
| $mime_type = get_post_mime_type( $post_id ); | |
| // If it’s an image attachment, use the original thumbnail | |
| if ( strpos( $mime_type, "image" ) !== false ) { | |
| $html = wp_get_attachment_image( $post_id, 'thumbnail' ); | |
| } | |
| } | |
| return $html; | |
| }, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment