WordPress: Mengatur panjang artikel berita
January 26 | Posted by Muhammad Yusuf E. | Blog Komunitas Tags: excerp, tip, wordpress
Artikel yang ditampilkan di depan halaman sebuah website, kadangkala tidak perlu panjang-panjang. Dengan demikian pembaca dapat dengan mudah memahami ringkasan artikel yang ingin dibacanya. Untuk mengatur panjang resume sebuah artikel, anda dapat menggunakan file functions.php yang ada di theme wordpress anda.
Edit file functions.php dan masukkanlah script php seperti berikut ini.
[sourcecode lang='php']
// panjang berita
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return 30; // Or whatever you want the length to be.
}
?>
[/sourcecode]
Script di atas, membatasi jumlah word yang ditampilkan menjadi 30 kata. Bagi anda yang tidak memakai WordPress versi 2.7, silakan memakai script seperti berikut ini.
[sourcecode lang='php']
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘custom_trim_excerpt’);
function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = x;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘…’);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
[/sourcecode]
Mudah bukan ? selamat mencoba.


