
Admin
Well-Known Member
Staff member
Administrator
Breadcrumbs là phần mở rộng của website giúp người sử dụng điều hướng tốt hơn. Để thêm vào WP ta các plugin mà không cần viết function như Breadcrumb NavXT hay Yoast Breadcrumbs . Ở bài viết này tôi xin giới thiệu các tùy biến Breadcrumbs mà không cần sử dụng các plugin nêu trên.
Đầu tiên chúng ta cần tạo một file có tên gọi là breadcrumb.php trong theme đang sử dụng. Tiếp theo ta gọi nó thông qua functions.php.
Tạo breadcrumb.php
Gọi breadcrumbs bằng cách thêm vào functions.php
Để hiển thị breadcrumbs bất cứ nơi nào ta muốn ta dùng hàm:
Để make-up breadcrumb ta có thể tối ưu hóa thẻ current:
Chúc bạn thành công
Đầu tiên chúng ta cần tạo một file có tên gọi là breadcrumb.php trong theme đang sử dụng. Tiếp theo ta gọi nó thông qua functions.php.
Tạo breadcrumb.php
PHP:
<?php
/*
Custom breadcrumbs
*/
function wp_breadcrumbs(){
$delimiter = '»';
$name = 'Home';
$currentBefore = '<span class="current">';
$currentAfter = '</span>';
if(!is_home() && !is_front_page() || is_paged()){
global $post;
$home = get_bloginfo('url');
echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
if(is_tax()){
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
echo $currentBefore . $term->name . $currentAfter;
} elseif (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 . '';
single_cat_title();
echo '' . $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()){
$postType = get_post_type();
if($postType == 'post'){
$cat = get_the_category(); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
} elseif($postType == 'portfolio'){
$terms = get_the_term_list($post->ID, 'portfolio-category', '', '###', '');
$terms = explode('###', $terms);
echo $terms[0]. ' ' . $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:', 'wpinsite'). ' "' . get_search_query() . '"' . $currentAfter;
} elseif (is_tag()){
echo $currentBefore . __('Post Tagged with:', 'wpinsite'). ' "';
single_tag_title();
echo '"' . $currentAfter;
} elseif (is_author()) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . __('Author Archive', 'wpinsite') . $currentAfter;
} elseif (is_404()){
echo $currentBefore . __('Page Not Found', 'wpinsite') . $currentAfter;
}
if(get_query_var('paged')){
if(is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo ' ' . $delimiter . ' ' . __('Page') . ' ' . get_query_var('paged');
if(is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
}
}
?>
Code:
require_once('library/breadcrumb.php');
Code:
<?php wp_breadcrumb(); ?>
Code:
.current { font-weight:bold; }