Breadcumb (thanh điều hướng) là một chức năng hầu như website nào cũng phải có. Hôm nay tôi sẽ chia sẽ với anh chị đoạn code tạo breadcrumb trên mọi page, anh chị chỉ cần đêm nó về rồi build html của thanh breadcrumb mà thôi
php code
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
function dulich_breadcrumb(){ if(!is_front_page()): $breadcrumb=array(); //Home $breadcrumb_item=array( 'url' => home_url(), 'title' => __('Home','chilitheme') ); array_push($breadcrumb,$breadcrumb_item); //at singular template if(is_singular() && !is_singular('product') && !is_page()): global $post; //Term $taxonomy_names = get_object_taxonomies($post); //var_dump(get_post_type()); $term_arr=array(); $terms = get_the_terms( $post->ID, $taxonomy_names[0] ); if($terms): foreach ( $terms as $term ): //get parent term top $term_parent_arr=get_ancestors($term->term_id,$taxonomy_names[0]); if($term_parent_arr): foreach($term_parent_arr as $item): $term_item = get_term($item,$taxonomy_names[0]); if($term_item->parent == 0): array_push($term_arr,$item); break; endif; endforeach; endif; array_push($term_arr,$term->term_id); endforeach; endif; $term_arr = array_unique($term_arr); if($term_arr): foreach($term_arr as $item): $term = get_term($item,$taxonomy_names[0]); $breadcrumb_item=array( 'url' => get_term_link( (int) $term->term_id,$taxonomy_names[0]), 'title' => $term->name ); array_push($breadcrumb,$breadcrumb_item); endforeach; endif; //singular $breadcrumb_item=array( 'url' => get_permalink($post->ID), 'title' => $post->post_title ); array_push($breadcrumb,$breadcrumb_item); endif; //page if(is_page()): global $post; $breadcrumb_item=array( 'url' => get_permalink($post->ID), 'title' => $post->post_title ); array_push($breadcrumb,$breadcrumb_item); endif; //blog if(is_home()): $page_for_posts=get_option('page_for_posts'); $breadcrumb_item=array( 'url' => get_permalink($page_for_posts), 'title' => __('Blog','chilitheme') ); array_push($breadcrumb,$breadcrumb_item); endif; //at taxonomy template if((is_tax() || is_category()) && !is_tax('product_cat')): global $wp_query; $term = $wp_query->get_queried_object(); $term_arr=array(); //get parent term top $taxonomy_names = get_object_taxonomies('post'); $term_parent_arr=get_ancestors($term->term_id,$taxonomy_names[0]); if($term_parent_arr): foreach($term_parent_arr as $item): $term_item = get_term($item,$taxonomy_names[0]); if($term_item->parent == 0): array_push($term_arr,$item); break; endif; endforeach; endif; array_push($term_arr,$term->term_id); $term_arr = array_unique($term_arr); if($term_arr): foreach($term_arr as $item): $term = get_term($item,$taxonomy_names[0]); $breadcrumb_item=array( 'url' => get_term_link( (int) $term->term_id,$taxonomy_names[0]), 'title' => $term->name ); array_push($breadcrumb,$breadcrumb_item); endforeach; endif; endif; if(is_post_type_archive()): $get_current_post_type=get_post_type(); $archive=get_post_type_object($get_current_post_type); //var_dump($archive); $breadcrumb_item=array( 'url' => get_post_type_archive_link($get_current_post_type), 'title' => $archive->labels->menu_name ); array_push($breadcrumb,$breadcrumb_item); endif; endif; return $breadcrumb; } |
Sử dụng:
$breadcrumb=dulich_breadcrumb();
Và khi print_r($breadcrumb) sẽ cho một array để tiện cho anh chị xây dựng mã html của breadcrumb dựa trên array này:
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 |
Array ( [0] => Array ( [url] => http://localhost/chilitheme/phukienoto [title] => Trang chủ ) [1] => Array ( [url] => http://localhost/chilitheme/phukienoto/category/tin-tuc/ [title] => Tin tức ) [2] => Array ( [url] => http://localhost/chilitheme/phukienoto/category/tin-tuc/tin-tuc-xe-du-lich/ [title] => Tin tức xe du lịch ) [3] => Array ( [url] => http://localhost/chilitheme/phukienoto/can-canh-4-bo-body-kit-lung-linh-cho-toyota-fortuner-2017-2/ [title] => Cận cảnh 4 bộ Body Kit lung linh cho Toyota Fortuner 2017 ) ) |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress