Display a Single Image in Excerpt
-
I am using WordPress as an internal tool to document our interface design patterns. On the search results page I want to display the title of the post along with a single image related to the post. Using the functions.php file I have added this code:
<?php function improved_trim_excerpt( $text ) { if ( '' == $text ) { $permalink = get_permalink(); // my addition $text = get_the_content( '' ); $text = strip_shortcodes( $text ); $text = apply_filters( 'the_content', $text ); $text = preg_replace( '@<script[^>]*?> . *?</script>@si', '', $text ); $text = str_replace( ']]>', ']]>', $text ); $text = strip_tags( $text, '<b><em><i><p><strong><sub><sup><h1><h2><h3><img>' ); // allowed tags (change as needed) $excerpt_length = apply_filters( 'excerpt_length', 10 ); $words = explode( ' ', $text, $excerpt_length + 1 ); if ( count( $words ) > $excerpt_length ) { array_pop( $words ); $text = implode( ' ', $words ); $text = $text . $excerpt_more; $text = force_balance_tags( $text ); // my addition } } return $text; } remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); add_filter( 'get_the_excerpt', 'improved_trim_excerpt' ); ?>This allows the images to show up but it also shows other text. I only want images to appear. Thoughts?
Thanks in advance,
Grant Novey
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Display a Single Image in Excerpt’ is closed to new replies.