Straight up Functional?
Had a minor conundrum the other day involving some pretty standard WordPress type stuff.. as these things usually sometimes go, it was about 1:30 am before I actually had a working solution. Note, solutions below are not for beginners, you WILL break your theme if you don’t implement the code properly.
The problem seemed like a no-brainer at first, if you want to display a preview of a post WordPress offers two options, the_excerpt() and the_content(). If you are using the_content() you have to manually insert a ‘read more’ tag wherever you need it to appear when readers are viewing the appended version of your post. I opted for the_excerpt(), which nicely and dynamically generates a brief version of an article (or an optional, manually written excerpt), along with whatever other information you decide to include along with it in the loop.
BUT, out of the box the_excerpt() adds a nifty little blob to the end of each excerpt unless you fill in your excerpt manually, looks like this […] Not pretty… of course there are plugins to spice up your excerpts and all that, but whenever possible I like to do a little future-proofing by writing actual code that can be baked into a theme. A little logic or some-such thing. I liked the solution I found at WP Engineer (which has conveniently disappeared, replaced by information for not-yet-released WordPress 2.9), which when added to your functions.php file, supplants WordPress’ own the_excerpt() functionality. Replace ‘better_trim_excerpt’ with your own name if your feeling feisty…
<?php function better_trim_excerpt($text) { global $post; if ( '' == $text ) { $text = get_the_content(''); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text); $text = strip_tags($text); $excerpt_length = 45; $words = explode(' ', $text, $excerpt_length + 1); if (count($words)> $excerpt_length) { array_pop($words); array_push($words, '...');//'...' is what replaces the unsightly blob.. $text = implode(' ', $words); } } return $text; } ?>
Just one problem, WordPress’s wondrous auto-generated captioning was being stripped from the resulting html, but not the content of the caption (leaving hideous random text willy-nilly). Add this bit of code to our previous solution, and problem solved..
<pre>add_filter( 'img_caption_shortcode', create_function('$a, $b, $c', 'return $c;'), 10, 3 );</pre>
which gives us the following;
<?php function better_trim_excerpt($text) { global $post; if ( '' == $text ) { add_filter( 'img_caption_shortcode', create_function('$a, $b, $c', 'return $c;'), 10, 3 ); $text = get_the_content(''); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text); $text = strip_tags($text); $excerpt_length = 45; $words = explode(' ', $text, $excerpt_length + 1); if (count($words)> $excerpt_length) { array_pop($words); array_push($words, '...');//'...' is what replaces the unsightly blob.. $text = implode(' ', $words); } } return $text; } ?> <?php remove_filter('get_the_excerpt', 'wp_trim_excerpt');//removes wp hook for excerpt function add_filter('get_the_excerpt', 'better_trim_excerpt');//points wordpress to yours, name this whatever you want! ?>
Having gone through all of this, today I discovered that the_excerpt() is bound for a makeover, and with the release of 2.9 two lines of code may accomplish what took me twenty-three or so. Though I’m not positive it will solve my caption problems…
Tags: php, the excerpt, theme development, wordpress