29 November 2010

Categorized | , , ,

Membuat Breadcrumb di Wordpress Tanpa Plugin

07:48

Breadcrumb adalah salah satu bentuk navigasi. Biasanya digunakan untuk memberitahukan user dimana posisi dia berada sekarang. Dengan adanya breadcrumb maka akan memudahkan user untuk berpindah ke top level dari current page.

Di Wordpress memang sudah ada beberapa plugin untuk membuat Breadcrumb ini contohnya Yoast Breadcrumbs, Breadcrumb Trail, WordPress Breadcrumbs dan masih banyak yang lain lagi. tapi bagaimana jika kita ingin membuat sendiri tanpa menggunakan plugin tambahan.

Disini saya akan coba jelaskan cara membuatnya, yaitu dengan menggunakan sebuat function, di hampir setiap theme wordpress terdapat file functions.php kita cukup menambahkan function tersebut kedalam file itu.

Berikut script yang kita masukan :


function caturla_breadcrumbs() {


$delimiter = '»';
$name = 'Home'; //text for the 'Home' link
$currentBefore = '<span class="current">';
$currentAfter = '</span>';


if ( !is_home() && !is_front_page() || is_paged() ) {


echo '<div id="crumbs">';


global $post;
$home = get_bloginfo('url');
echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';


if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
echo $currentBefore . 'Archive by category &#39;';
single_cat_title();
echo '&#39;' . $currentAfter;


} elseif ( is_day() ) {
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $currentBefore . get_the_time('d') . $currentAfter;


} elseif ( is_month() ) {
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo $currentBefore . get_the_time('F') . $currentAfter;


} elseif ( is_year() ) {
echo $currentBefore . get_the_time('Y') . $currentAfter;


} elseif ( is_single() && !is_attachment() ) {
$cat = get_the_category(); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo $currentBefore;
the_title();
echo $currentAfter;


} elseif ( is_attachment() ) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
echo $currentBefore;
the_title();
echo $currentAfter;


} elseif ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;


} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $currentBefore;
the_title();
echo $currentAfter;


} elseif ( is_search() ) {
echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;


} elseif ( is_tag() ) {
echo $currentBefore . 'Posts tagged &#39;';
single_tag_title();
echo '&#39;' . $currentAfter;


} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;


} elseif ( is_404() ) {
echo $currentBefore . 'Error 404' . $currentAfter;
}


if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo __('Page') . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}


echo '</div>';


}
}

Simpan file tersebut di file functions.php, kemudian buka file single.php untuk menampilkan breadcrumb. di file single.php cari kode

<?php the_title(); ?>

lalu pastekan kode berikut diatas title.

<?php if (function_exists('caturla_breadcrumbs')) caturla_breadcrumbs(); ?>

Kemudian save file single.php dan lihat hasilnya.

Selamat Mencoba :).



Share/Bookmark

6 Responses to “Membuat Breadcrumb di Wordpress Tanpa Plugin”

santi mengatakan...
Senin, 29 November, 2010

nice post


caturla.web.id mengatakan...
Senin, 29 November, 2010

Thank's for visiting my blog :) ...


Galuh Ristyanto mengatakan...
Selasa, 14 Desember, 2010

Aku pilih pke plugin aja deh. Biar gampang mantaunya. Klo tanpa plugin kadang kelupaan apa aja yg ditambahkan ke code-nya.


caturla.web.id mengatakan...
Rabu, 15 Desember, 2010

iya contoh ini cuma buat pilihan bro, sambil kita belajar coding di WP :), tapi semua tergantung ke pilihan masing-masing.


akhatam mengatakan...
Jumat, 07 Januari, 2011

kira2 mmperberat blog ga tuh mas kalo pke kode sbnyak itu??


arkhananta mengatakan...
Minggu, 21 Agustus, 2011

thanks infonya mas, dengan cara ini bisa memperkecil penggunan plugin..


Poskan Komentar