WordPress Gelişmiş the_excerpt(); Fonksiyonu

WordPress index sayfamızda tasarımla uyumlu ve karışıklık olmaması için yazının bir kısmını bölmek gerekebiliyor. Bunu php content kodu kullanarak more ile böleceğimize makalemizi yazıp otomatik bölmesini sağlayabiliyoruz. Yani bu fonksiyonun mantığı yazıyı bölmek değil Html kodlarını göstermemek ve yazınının belirlediğimiz kelime sınırlaması ile kesmesidir. Peki bunu nasıl kullanacağız derseniz aşağıda fonksiyon kodları ve index sayfamızda kullanacağımız kod parçacığını vereceğim.

Temanızın functions.php dosyasını açın ve kodundan sonra bu kodları ekleyin.

function print_excerpt($length) {
	global $post;
	$text = $post->post_excerpt;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
	}
	$text = strip_shortcodes($text);
	$text = strip_tags($text);
 
	$text = substr($text,0,$length);
	$excerpt = reverse_strrchr($text, '.', 1);
	if( $excerpt ) {
		echo apply_filters('the_excerpt',$excerpt);
	} else {
		echo apply_filters('the_excerpt',$text);
	}
}
function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}

Yorum yap