{"id":12876,"date":"2017-12-16T13:26:04","date_gmt":"2017-12-16T06:26:04","guid":{"rendered":"https:\/\/www.pugam.com\/?p=12876"},"modified":"2020-01-02T00:42:54","modified_gmt":"2020-01-01T17:42:54","slug":"cara-membuat-breadcrumbs-di-wordpress-tanpa-bantuan-plugin","status":"publish","type":"post","link":"https:\/\/www.pugam.com\/12876\/cara-membuat-breadcrumbs-di-wordpress-tanpa-bantuan-plugin\/","title":{"rendered":"Cara Membuat Breadcrumbs di WordPress tanpa Plugin"},"content":{"rendered":"
<\/p>\n
Sudah tahu apa itu breadcrumbs<\/em>? Jika diartikan ke dalam bahasa Indonesia breadcrumbs<\/em> memiliki arti “remah roti”. Dalam film animasi klasik terutama ketika adegan si tokoh sedang melakukan pencarian, ceceran remah roti sering digambarkan sebagai sebuah petunjuk.<\/p>\n Seperti itu juga lah peran breadcrumbs<\/em> di dalam sebuah website\/blog dan karena itu pula kenapa fungsi ini disebut sebagai breadcrumbs<\/em>.<\/p>\n Breadcrumbs<\/em> adalah fungsi untuk menampilkan hierarki<\/a> dari halaman yang sedang Anda buka. Contoh, dalam situs pugam.com ini Anda bisa melihat breadcrumbs<\/em> di atas judul artikel, jika diurut kebelakang maka Anda akan diarahkan ke halaman depan atau home<\/strong> situs ini.<\/p>\n <\/p>\n Tidak semua tema di WordPress menyertakan fungsi ini, atau WordPress pun secara default<\/em> tidak akan menambahkan fungsi ini ke dalam situs Anda. Metode paling umum ketika Anda ingin menambahkan breadcrumbs<\/em> adalah dengan menggunakan plugin.<\/p>\n Tapi di sini saya akan menunjukkan cara bagaimana menambahkan breadcrumbs<\/em> di WordPress tanpa bantuan plugin. Mungkin Anda bertanya, kenapa sih repot-repot padahal kan tinggal pakai plugin, beres!?<\/p>\n Kalau saya, ketika saya menggunakan WordPress maka penggunaan plugin itu sebisa mungkin diminimalisir. Banyak plugin yang menyertakan fungsi atau opsi yang sebetulnya tidak benar-benar Anda butuhkan disamping fungsi utamanya.<\/p>\n Sedangkan jika Anda melakukannya secara manual, yaitu dengan membuat script<\/em> sendiri maka tentu Anda tidak akan menambahkan fungsi\/script<\/em> yang tidak Anda butuhkan bukan?<\/p>\n Untuk script<\/em> yang akan saya bagikan kali ini sudah mendukung:<\/p>\n Silakan tambahkan script<\/em> di bawah ini ke dalam file functions.php\u00a0<\/strong>dan\u00a0style.css<\/strong>.<\/p>\n Kemudian panggil fungsi di atas dengan menambahkan kode di bawah ini ke dalam file template halaman, tergantung di mana Anda ingin menampilkannya, apakah di single post atau page, setiap tema biasanya memisahkan file template antara single post dan page.<\/p>\n Berikut adalah contoh ketika saya menerapkannya di situs pugam.com ini, di mana breadcrumbs<\/em> paling atas dihasilkan dari plugin SEO by Yoast dan breadcrumbs<\/em> di bawahnya dihasilkan dengan menggunakan script<\/em> di atas.<\/p>\n <\/p>\n Bagi Anda yang menggunakan plugin SEO by Yoast<\/strong>, Anda dapat mengaktifkan fungsi breadcrumbs<\/em> ini dengan mudah. Caranya, di menu pengaturan <\/strong>plugin SEO by Yoast, masuk ke bagian Advanced<\/strong>, nanti Anda akan menemukan bagian breadcrumbs<\/em>.<\/p>\n <\/p>\n Pada bagian Enable Breadcrumbs<\/strong>, geser toggle<\/em> menjadi Enabled<\/strong> lalu atur dan sesuaikan opsi-opsi di bawahnya. Terakhir, untuk menampilkannya tambahkan kode berikut di halaman yang Anda inginkan:<\/p>\nPengertian Breadcrumbs dalam Sebuah Website\/Blog<\/h2>\n
Cara Menambakan Breadcrumbs di WordPress tanpa Bantuan Plugin<\/h2>\n
\n
\/\/Breadcrumbs\r\nfunction custom_breadcrumbs() {\r\n\r\n \/\/ Settings\r\n $separator = '>';\r\n $breadcrums_id = 'breadcrumbs';\r\n $breadcrums_class = 'breadcrumbs';\r\n $home_title = 'Homepage';\r\n\r\n \/\/ If you have any custom post types with custom taxonomies, put the taxonomy name below (e.g. product_cat)\r\n $custom_taxonomy = 'product_cat';\r\n\r\n \/\/ Get the query & post information\r\n global $post, $wp_query;\r\n\r\n \/\/ Do not display on the homepage\r\n if (!is_front_page()) {\r\n\r\n \/\/ Build the breadcrums\r\n echo '<ul id=\"'.$breadcrums_id.\r\n '\" class=\"'.$breadcrums_class.\r\n '\">';\r\n\r\n \/\/ Home page\r\n echo '<li class=\"item-home\"><a class=\"bread-link bread-home\" href=\"'.get_home_url().\r\n '\" title=\"'.$home_title.\r\n '\">'.$home_title.\r\n '<\/a><\/li>';\r\n echo '<li class=\"separator separator-home\"> '.$separator.\r\n ' <\/li>';\r\n\r\n if (is_archive() && !is_tax() && !is_category() && !is_tag()) {\r\n\r\n echo '<li class=\"item-current item-archive\"><strong class=\"bread-current bread-archive\">'.post_type_archive_title($prefix, false).\r\n '<\/strong><\/li>';\r\n\r\n } else if (is_archive() && is_tax() && !is_category() && !is_tag()) {\r\n\r\n \/\/ If post is a custom post type\r\n $post_type = get_post_type();\r\n\r\n \/\/ If it is a custom post type display name and link\r\n if ($post_type != 'post') {\r\n\r\n $post_type_object = get_post_type_object($post_type);\r\n $post_type_archive = get_post_type_archive_link($post_type);\r\n\r\n echo '<li class=\"item-cat item-custom-post-type-'.$post_type.\r\n '\"><a class=\"bread-cat bread-custom-post-type-'.$post_type.\r\n '\" href=\"'.$post_type_archive.\r\n '\" title=\"'.$post_type_object - > labels - > name.\r\n '\">'.$post_type_object - > labels - > name.\r\n '<\/a><\/li>';\r\n echo '<li class=\"separator\"> '.$separator.\r\n ' <\/li>';\r\n\r\n }\r\n\r\n $custom_tax_name = get_queried_object() - > name;\r\n echo '<li class=\"item-current item-archive\"><strong class=\"bread-current bread-archive\">'.$custom_tax_name.\r\n '<\/strong><\/li>';\r\n\r\n } else if (is_single()) {\r\n\r\n \/\/ If post is a custom post type\r\n $post_type = get_post_type();\r\n\r\n \/\/ If it is a custom post type display name and link\r\n if ($post_type != 'post') {\r\n\r\n $post_type_object = get_post_type_object($post_type);\r\n $post_type_archive = get_post_type_archive_link($post_type);\r\n\r\n echo '<li class=\"item-cat item-custom-post-type-'.$post_type.\r\n '\"><a class=\"bread-cat bread-custom-post-type-'.$post_type.\r\n '\" href=\"'.$post_type_archive.\r\n '\" title=\"'.$post_type_object - > labels - > name.\r\n '\">'.$post_type_object - > labels - > name.\r\n '<\/a><\/li>';\r\n echo '<li class=\"separator\"> '.$separator.\r\n ' <\/li>';\r\n\r\n }\r\n\r\n \/\/ Get post category info\r\n $category = get_the_category();\r\n\r\n if (!empty($category)) {\r\n\r\n \/\/ Get last category post is in\r\n $last_category = end(array_values($category));\r\n\r\n \/\/ Get parent any categories and create array\r\n $get_cat_parents = rtrim(get_category_parents($last_category - > term_id, true, ','), ',');\r\n $cat_parents = explode(',', $get_cat_parents);\r\n\r\n \/\/ Loop through parent categories and store in variable $cat_display\r\n $cat_display = '';\r\n foreach($cat_parents as $parents) {\r\n $cat_display. = '<li class=\"item-cat\">'.$parents.\r\n '<\/li>';\r\n $cat_display. = '<li class=\"separator\"> '.$separator.\r\n ' <\/li>';\r\n }\r\n\r\n }\r\n\r\n \/\/ If it's a custom post type within a custom taxonomy\r\n $taxonomy_exists = taxonomy_exists($custom_taxonomy);\r\n if (empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {\r\n\r\n $taxonomy_terms = get_the_terms($post - > ID, $custom_taxonomy);\r\n $cat_id = $taxonomy_terms[0] - > term_id;\r\n $cat_nicename = $taxonomy_terms[0] - > slug;\r\n $cat_link = get_term_link($taxonomy_terms[0] - > term_id, $custom_taxonomy);\r\n $cat_name = $taxonomy_terms[0] - > name;\r\n\r\n }\r\n\r\n \/\/ Check if the post is in a category\r\n if (!empty($last_category)) {\r\n echo $cat_display;\r\n echo '<li class=\"item-current item-'.$post - > ID.\r\n '\"><strong class=\"bread-current bread-'.$post - > ID.\r\n '\" title=\"'.get_the_title().\r\n '\">'.get_the_title().\r\n '<\/strong><\/li>';\r\n\r\n \/\/ Else if post is in a custom taxonomy\r\n } else if (!empty($cat_id)) {\r\n\r\n echo '<li class=\"item-cat item-cat-'.$cat_id.\r\n ' item-cat-'.$cat_nicename.\r\n '\"><a class=\"bread-cat bread-cat-'.$cat_id.\r\n ' bread-cat-'.$cat_nicename.\r\n '\" href=\"'.$cat_link.\r\n '\" title=\"'.$cat_name.\r\n '\">'.$cat_name.\r\n '<\/a><\/li>';\r\n echo '<li class=\"separator\"> '.$separator.\r\n ' <\/li>';\r\n echo '<li class=\"item-current item-'.$post - > ID.\r\n '\"><strong class=\"bread-current bread-'.$post - > ID.\r\n '\" title=\"'.get_the_title().\r\n '\">'.get_the_title().\r\n '<\/strong><\/li>';\r\n\r\n } else {\r\n\r\n echo '<li class=\"item-current item-'.$post - > ID.\r\n '\"><strong class=\"bread-current bread-'.$post - > ID.\r\n '\" title=\"'.get_the_title().\r\n '\">'.get_the_title().\r\n '<\/strong><\/li>';\r\n\r\n }\r\n\r\n } else if (is_category()) {\r\n\r\n \/\/ Category page\r\n echo '<li class=\"item-current item-cat\"><strong class=\"bread-current bread-cat\">'.single_cat_title('', false).\r\n '<\/strong><\/li>';\r\n\r\n } else if (is_page()) {\r\n\r\n \/\/ Standard page\r\n if ($post - > post_parent) {\r\n\r\n \/\/ If child page, get parents \r\n $anc = get_post_ancestors($post - > ID);\r\n\r\n \/\/ Get parents in the right order\r\n $anc = array_reverse($anc);\r\n\r\n \/\/ Parent page loop\r\n if (!isset($parents)) $parents = null;\r\n foreach($anc as $ancestor) {\r\n $parents. = '<li class=\"item-parent item-parent-'.$ancestor.\r\n '\"><a class=\"bread-parent bread-parent-'.$ancestor.\r\n '\" href=\"'.get_permalink($ancestor).\r\n '\" title=\"'.get_the_title($ancestor).\r\n '\">'.get_the_title($ancestor).\r\n '<\/a><\/li>';\r\n $parents. = '<li class=\"separator separator-'.$ancestor.\r\n '\"> '.$separator.\r\n ' <\/li>';\r\n }\r\n\r\n \/\/ Display parent pages\r\n echo $parents;\r\n\r\n \/\/ Current page\r\n echo '<li class=\"item-current item-'.$post - > ID.\r\n '\"><strong title=\"'.get_the_title().\r\n '\"> '.get_the_title().\r\n '<\/strong><\/li>';\r\n\r\n } else {\r\n\r\n \/\/ Just display current page if not parents\r\n echo '<li class=\"item-current item-'.$post - > ID.\r\n '\"><strong class=\"bread-current bread-'.$post - > ID.\r\n '\"> '.get_the_title().\r\n '<\/strong><\/li>';\r\n\r\n }\r\n\r\n } else if (is_tag()) {\r\n\r\n \/\/ Tag page\r\n\r\n \/\/ Get tag information\r\n $term_id = get_query_var('tag_id');\r\n $taxonomy = 'post_tag';\r\n $args = 'include='.$term_id;\r\n $terms = get_terms($taxonomy, $args);\r\n $get_term_id = $terms[0] - > term_id;\r\n $get_term_slug = $terms[0] - > slug;\r\n $get_term_name = $terms[0] - > name;\r\n\r\n \/\/ Display the tag name\r\n echo '<li class=\"item-current item-tag-'.$get_term_id.\r\n ' item-tag-'.$get_term_slug.\r\n '\"><strong class=\"bread-current bread-tag-'.$get_term_id.\r\n ' bread-tag-'.$get_term_slug.\r\n '\">'.$get_term_name.\r\n '<\/strong><\/li>';\r\n\r\n }\r\n elseif(is_day()) {\r\n\r\n \/\/ Day archive\r\n\r\n \/\/ Year link\r\n echo '<li class=\"item-year item-year-'.get_the_time('Y').\r\n '\"><a class=\"bread-year bread-year-'.get_the_time('Y').\r\n '\" href=\"'.get_year_link(get_the_time('Y')).\r\n '\" title=\"'.get_the_time('Y').\r\n '\">'.get_the_time('Y').\r\n ' Archives<\/a><\/li>';\r\n echo '<li class=\"separator separator-'.get_the_time('Y').\r\n '\"> '.$separator.\r\n ' <\/li>';\r\n\r\n \/\/ Month link\r\n echo '<li class=\"item-month item-month-'.get_the_time('m').\r\n '\"><a class=\"bread-month bread-month-'.get_the_time('m').\r\n '\" href=\"'.get_month_link(get_the_time('Y'), get_the_time('m')).\r\n '\" title=\"'.get_the_time('M').\r\n '\">'.get_the_time('M').\r\n ' Archives<\/a><\/li>';\r\n echo '<li class=\"separator separator-'.get_the_time('m').\r\n '\"> '.$separator.\r\n ' <\/li>';\r\n\r\n \/\/ Day display\r\n echo '<li class=\"item-current item-'.get_the_time('j').\r\n '\"><strong class=\"bread-current bread-'.get_the_time('j').\r\n '\"> '.get_the_time('jS').\r\n ' '.get_the_time('M').\r\n ' Archives<\/strong><\/li>';\r\n\r\n } else if (is_month()) {\r\n\r\n \/\/ Month Archive\r\n\r\n \/\/ Year link\r\n echo '<li class=\"item-year item-year-'.get_the_time('Y').\r\n '\"><a class=\"bread-year bread-year-'.get_the_time('Y').\r\n '\" href=\"'.get_year_link(get_the_time('Y')).\r\n '\" title=\"'.get_the_time('Y').\r\n '\">'.get_the_time('Y').\r\n ' Archives<\/a><\/li>';\r\n echo '<li class=\"separator separator-'.get_the_time('Y').\r\n '\"> '.$separator.\r\n ' <\/li>';\r\n\r\n \/\/ Month display\r\n echo '<li class=\"item-month item-month-'.get_the_time('m').\r\n '\"><strong class=\"bread-month bread-month-'.get_the_time('m').\r\n '\" title=\"'.get_the_time('M').\r\n '\">'.get_the_time('M').\r\n ' Archives<\/strong><\/li>';\r\n\r\n } else\r\n if (is_year()) {\r\n\r\n \/\/ Display year archive\r\n echo '<li class=\"item-current item-current-'.get_the_time('Y').\r\n '\"><strong class=\"bread-current bread-current-'.get_the_time('Y').\r\n '\" title=\"'.get_the_time('Y').\r\n '\">'.get_the_time('Y').\r\n ' Archives<\/strong><\/li>';\r\n\r\n } else if (is_author()) {\r\n\r\n \/\/ Auhor archive\r\n\r\n \/\/ Get the author information\r\n global $author;\r\n $userdata = get_userdata($author);\r\n\r\n \/\/ Display author name\r\n echo '<li class=\"item-current item-current-'.$userdata - > user_nicename.\r\n '\"><strong class=\"bread-current bread-current-'.$userdata - > user_nicename.\r\n '\" title=\"'.$userdata - > display_name.\r\n '\">'.\r\n 'Author: '.$userdata - > display_name.\r\n '<\/strong><\/li>';\r\n\r\n } else if (get_query_var('paged')) {\r\n\r\n \/\/ Paginated archives\r\n echo '<li class=\"item-current item-current-'.get_query_var('paged').\r\n '\"><strong class=\"bread-current bread-current-'.get_query_var('paged').\r\n '\" title=\"Page '.get_query_var('paged').\r\n '\">'.__('Page').\r\n ' '.get_query_var('paged').\r\n '<\/strong><\/li>';\r\n\r\n } else if (is_search()) {\r\n\r\n \/\/ Search results page\r\n echo '<li class=\"item-current item-current-'.get_search_query().\r\n '\"><strong class=\"bread-current bread-current-'.get_search_query().\r\n '\" title=\"Search results for: '.get_search_query().\r\n '\">Search results for: '.get_search_query().\r\n '<\/strong><\/li>';\r\n\r\n }\r\n elseif(is_404()) {\r\n\r\n \/\/ 404 page\r\n echo '<li>'.\r\n 'Error 404'.\r\n '<\/li>';\r\n }\r\n\r\n echo '<\/ul>';\r\n\r\n }\r\n\r\n}<\/pre>\n
#breadcrumbs{\r\n list-style:none;\r\n margin:10px 0;\r\n overflow:hidden;\r\n}\r\n \r\n#breadcrumbs li{\r\n display:inline-block;\r\n vertical-align:middle;\r\n margin-right:15px;\r\n}\r\n \r\n#breadcrumbs .separator{\r\n font-size:18px;\r\n font-weight:100;\r\n color:#ccc;\r\n}<\/pre>\n
<?php custom_breadcrumbs(); ?><\/pre>\n
Bonus<\/h2>\n
<?php if ( function_exists('yoast_breadcrumb') ) {yoast_breadcrumb('<p id=\"breadcrumbs\">','<\/p>');} ?>
<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Sudah tahu apa itu breadcrumbs? Jika diartikan ke dalam bahasa Indonesia breadcrumbs memiliki arti “remah roti”. Dalam film animasi klasik \n ...<\/a>","protected":false},"author":3,"featured_media":12877,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","two_page_speed":[]},"categories":[2092],"yoast_head":"\n