Beim Schreiben eines „Related Posts“-Plugins für ein neues Theme musste ich feststellen dass es in WordPress keine Funktion gibt welche den Auszug (Excerpt) für eine Seite bzw. einen Artikel in Abhängigkeit der Artikelnummer ($post_id) lesen oder erstellen kann. Also habe ich eine entsprechende Funktion geschrieben und gleich den „excerpt_more„-Filter aus der zukünftigen aktuellen Version 2.9 implementiert.
Function get_the_excerpt($post_id = Null){
If ($post_id == Null) $post_id = get_the_ID();
// Get the post
$post = get_post ($post_id);
// check if password is required
If ( post_password_required($post) ){
$excerpt = __('There is no excerpt because this is a protected post.'); // translation in the core text domain
return $excerpt;
}
If ($post->post_excerpt != ''){
// get the users excerpt
$excerpt = $post->post_excerpt;
}
Else {
// Create the excerpt
$excerpt = $post->post_content;
$excerpt = strip_shortcodes( $excerpt );
$excerpt = apply_filters('the_content', $excerpt);
$excerpt = str_replace(']]>', ']]>', $excerpt);
$excerpt = strip_tags($excerpt);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', '[...]');
$words = explode(' ', $excerpt, $excerpt_length + 1);
If ( Count($words) > $excerpt_length ){
Array_Pop($words);
Array_Push($words, $excerpt_more);
$excerpt = Implode(' ', $words);
}
}
// Run filters - there is a bug if you try to run an empty excerpt through the filter
If ($excerpt != '') $excerpt = apply_filters('get_the_excerpt', $excerpt);
return $excerpt;
}
Die Funktion heißt genau wie das Original „get_the_excerpt“ und kann die aktuelle get_the_excerpt aus post-template.php nicht überschreiben. Die neue Funktion muss also umbenannt oder, wie ich es empfehlen würde, in einer Klasse deklariert werden.