Gerade wo es um Bücher und Autoren geht, ist es hilfreich, wenn die Suchergebnisse nicht nur Text, sondern auch Bilder (Covers, Autorenfotos, Illustrationen) beinhalten. Gesichter, Grafiken und Schriftzüge werden mehr oder weniger bewusst wiedererkannt. Spuckt die Suche das passende Bild also gleich mit aus, ist allen geholfen.
Am einfachsten lässt sich das in WordPress über die Featured Images (auch: Post Thumbnails) bewerkstelligen, die sich für ein Bücherportfolio ohnehin anbieten. Ich möchte darüber hinaus auch alle statischen Seiten — die in der Regel kein Featured Image beinhalten — in die Bildersuche miteinbeziehen.
Denn selbst wenn das nur einige Dekorationsbilder zum Vorschein bringen sollte: Das Suchergebnis sieht freundlicher aus. Und vielleicht erhält so auch ein anderer Titel die Aufmerksamkeit, die er sonst nicht bekommen hätte.
search.php für Genesis
<?php /* * Custom Genesis search results page template * */ //* Add body class of "archive" to simplify styling add_filter( 'body_class', function( $classes ) { $classes[] = 'archive'; return $classes; }); //* Add search results page title add_action( 'genesis_before_content', function() { $title = sprintf( '<div class="entry-header"><h1 class="entry-title">%s%s%s</h1></div>', 'Ergebnisse für: "', get_search_query(), '"' ); echo $title; }); //* Add Featured Image to post results and first image of page to page results // Fetch first image of page/post function bub_fetch_first_image() { // Prepare search for image global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); // Search for images via Regex and store the first found image in a variable $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches[1][0]; // Show default image if nothing was found if(empty($first_img)) { $first_img = "DOMAIN/PATH/TO/IMAGE/default-image.png"; } // Return first image return $first_img; } //* Add proper images to search results add_action( 'genesis_entry_header', 'bub_search_results_featured_images', 8 ); function bub_search_results_featured_images() { // If featured image is set use it ... if ( has_post_thumbnail() ) { $thumb = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'wp-post-image' ) ) ); $featured_image = sprintf( '<a href="%s">%s</a>', get_permalink(), $thumb ); echo $featured_image; } // ... otherwise use the first found image in the page else { $first_in_page = sprintf( '<a href="%s"><img src="%s" class="wp-post-image" alt=""></a>', get_permalink(), bub_fetch_first_image() ); echo $first_in_page; } } genesis();
Links
- Get first image from a post auf CSS-Tricks