WordPress: function to format teaser different from body
On many sites that I develop I want to show articles that contains both a teaser text and a body text, and I want to be able to format the teaser different than the body. So…I made a function to accomplish this.
What the function does is that it wraps everything in the post before the ”read more”-tag in a div with the class "post-teaser", and everything else in a div with the class "post-body". Then it’s a piece of cake to add som styles to those divs with CSS.
I like it and use it a lot. And here it is:
/**
* This is a nifty little finction that makes is possible to
* format teaser and body differently
*
* It will output:
* - teaser/text before the read-more-thingie wrapped in div.post-teaser
* - body/text after the read-more-thingie wrapped in div.post-body
* - ..but only if each one exists. so you can get teaser + body, or only teaser, or only body
*
* @author Pär Thernström
*
*/
function ma_teaser_and_body($post_id = NULL) {
global $post, $more;
$post_org = $post;
$more_org = $more;
if (!$post_id) {
$post_id = $post->ID;
}
$post = get_post($post_id);
setup_postdata($post);
// Get teaser/text before the "read me"
$content = $post->post_content;
if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
// more-tag exists
$more = 0;
ob_start();
the_content("", true);
$teaser = ob_get_clean();
} else {
$teaser = "";
}
// Get the content/text after "read me"
$more = 1;
ob_start();
the_content(NULL, true);
$body = ob_get_clean();
if ($teaser) {
$teaser = "<div class='post-teaser'>$teaser</div>";
}
if ($body) {
$body = "<div class='post-body'>$body</div>";
}
$post = $post_org;
$more = $more_org;
setup_postdata($post);
echo $teaser . $body;
}
Changelog
27 mar 2011: Updated code a bit due to a problem that occured when no more-tag existed in post.
CMS Tree Page View recommended at WordCamp Australia
Troy Dean of Video User Manuals did a presentation at WordCamp Australia called ”Better WordPress For Clients”. About 16 minutes in the talk he recommends my WordPress plugin CMS Tree Page View for managing pages in sites with more than 10 pages. Thanks for telling Australia, Troy! ![]()
WordPress 3.1: more CMS than ever
This week has been all about the new WordPress release: 3.1. It is a great release and some of the new features that I really like are the new add link popup that easy let’s you choose among your existing pages and the writing interface that now features a lot less panels than before (something I’ve been bothered by for a long time).
Also, as Matt says: ”With the 3.1 release, WordPress is more of a CMS than ever before”.
These words from Matt feels very good for me since most of my plugins for WordPress focus on extending it’s CMS functionality.
So: WordPress 3.1 + CMS Tree Page View + Simple Fields + Simple History = a heck of a CMS!
Jag har blivit en programköpare
Lika bra jag kommer ut ur garderoben: jag har blivit en person som köper datorprogram. Länge har jag hängt på forum och på the pirate bay för att få tag på fulla versioner av program, men efter att jag själv börjat skriva plugins till WordPress så har jag insett att man måste belöna de personer som sliter för att göra något bra.
Själv blir jag överlycklig så fort någon bloggar, twittrar eller donerar pengar till mina projekt och plugins. Så jag kan bara inte vara sämre själv och därför är min policy framöver att allt jag använder regelbundet ska jag betala för eller donera till.
Följande program och tjänster är några av de jag hittills köpt:
- AirFoil
- Spotify
- TotalFinder
- Dropbox
- Coda
- Simplenote
- Little Snitch
- Memories
- Changes
- MoveAddict (fast ersattes snabbt av TotalFinder som fick stöd för att flytta filer)
- flattr.com
- Voodoopad (dock ersatts av Simplenote numera)


