Tutorial PHP, Advanced, Dasar, MySQL, Database, Sistem Informasi, Linux, SMS Gateway, PDF, JPGraph, Grafik, Excel, Laporan

Excerpting Recent Post di sidebar


13-September-2011 ditulis oleh admin

Setelah kita kenal dengan Excerpt di Posting dan resume pada wordpress, maka kita pun sebenarnya bisa memotong title pada Recent Post Sidebar jika diperkirakan terlalu panjang. Selain dengan mencari pluggins, kita bisa melakukannya dengan : Editing WordPress itu sendiri.

Theme WordPress
Biasanya paada Theme WordPress, jika tidak terdapat widget yang dipasang, maka Theme memiliki widget standard sendiri. Widget bawaan dari themes ini biasanya tersimpan di {dir_wp}/wp-contents/themes/nama_themes/sidebar.php. Kira-kira seperti demikian source code yang ada:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<h1><?php _e("Recent Posts"); ?></h1>
 
<ul>
 
<?php
 
foreach ($recentposts as $post) {
 
if ($post->post_title == '')
 
$post->post_title = sprintf(__('Post #%s'), $post->ID);
 
echo "<li><a href='".get_permalink($post->ID)."'>";
 
the_title_excerpt();
 
echo '</a></li>';
 
}
?>

Namun jika widget Recent Post dipasang, maka code diatas tidak lagi digunakan, melainkan akan menggunakan function widget() yang terdapat pada default-widget.php pada direktori wp-includes. Pada fungsi tersebut terdapat bagian :

1
2
3
4
5
6
7
8
9
10
<li>
<a 
href="<?php the_permalink() ?>" 
title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"
>
 
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
 
</a>
</li>

Perhatikan pada bagian the_title(). The title tersebut terletak pada file wp-includes/post-template.php, bukalah file tersebut. Saya melakukan penambahan fungsi untuk mempermudah membedakan antara keduanya. Carilah fungsi the_title() kira-kira akan didapatkan seperti demikian:

1
2
3
4
5
6
7
8
9
10
11
12
function the_title($before = '', $after = '', $echo = true) {
	$title = get_the_title();
 
	if ( strlen($title) == 0 )
		return;
 
	$title = $before . $title . $after;
	if ( $echo )
		echo $title;
	else
		return $title;
}

Coba lakukan seperti yang saya lakukan, tambahkan dibawah fungsi the title diatas tersebut menjadi seperti demikian:

Ingat! Copy dulu file tersebut sebelum mengubah… 2 file: post-template.php dan default-widget.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
function the_title($before = '', $after = '', $echo = true) {
	$title = get_the_title();
 
	if ( strlen($title) == 0 )
		return;
 
	$title = $before . $title . $after;
	if ( $echo )
		echo $title;
	else
		return $title;
}
 
 
function the_title_excerpt($before = '', $after = '', $echo = true) {
	$title_1 = get_the_title();
	//echo $title_1;
   $title_excerpt = substr($title_1, 0, 30);
//echo $title_excerpt;
	if ( strlen($title_excerpt) == 0 )
		return;
   if(strlen($title_1)>30){
   $dot = "...";
   }else{
   $dot = "";
   }
	$title_excerpt = $before . $title_excerpt . $after . $dot;
 
	if ( $echo )
		echo $title_excerpt;
	else
		return $title_excerpt;
}

Kita tambahkan sebuah fungsi bernama the_title_excerpt(), dimana the_title_excerpt() ini melakukan pengambilan sebanyak 30 hurup dari title yang didapat oleh fungsi get_the_title(); Jika hasilnya lebih dari jumlah hurup dalam judul artikel akan ditambahkan tanda …

Setelah selesai, buka kembali file default-widget.php, dan lakukan pengubahan dari

1
2
3
4
5
6
7
8
9
10
<li>
<a 
href="<?php the_permalink() ?>" 
title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"
>
 
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
 
</a>
</li>

menjadi

1
2
3
4
5
6
7
8
9
10
<li>
<a 
href="<?php the_permalink() ?>" 
title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"
>
 
<?php if ( get_the_title() ) the_title_excerpt(); else the_ID(); ?>
 
</a>
</li>

Simpan kedua file tersebut, dan pasanglah widget Recent Post anda, dan lihatlah apa yang terjadi untuk nama artikel yang lebih dari 30 karakter? Sukses? tentunya sukses.. Jika tidak, saya siap tanggung jawab hehehe…

Semoga Berguna

bimosaurus
—eof—



Terdapat dalam: PHP, wordpress | 3 Komentar »

3 Responses

  1. Excerpting Recent Post di sidebar « bimosaurus.wordpress.com Berkata:

    [...] http://bimosaurus.com/2011/09/13/excerpting-recent-post-di-sidebar/ [...]

  2. maztrieā„¢ Berkata:

    Anjritttt…..
    tampilan CMSmu edyanik Dabb….

    AKu njalukkk……

  3. bimosaurus Berkata:

    SIAAAAP ! ! ! !

Tulis Komentar

Perhatian: Komentar anda masuk dalam daftar moderasi, sehingga tertunda tampil, anda tidak harus mengulangi komentar anda.


Cari Artikel dalam Blog Ini

Menu

Popular Posts

Posting Terakhir

Kategori

Tag Bahasan

Linux and Images



####################
####################
####################
####################
####################
####################
####################
####################
####################
####################

Created by :
http://bimosaurus.com/imagefromtext/

Komentar Terakhir

Link terkait

Masukkan alamat email anda untuk berlangganan. Setiap post artikel anda akan mendapat notifikasi

Search


Anda membutuhkan tutorial WEB, SMS Gateway, Linux, Jaringan, PHP, MySQL dan Lainnya? Kirimkan melalui bimosaurus@gmail.com dan akan segera ditanggapi oleh team, GRATIS ! !