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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
function breadcrumb(){ if(!is_front_page()): $breadcrumb=array(); //Home $breadcrumb_item=array( 'url' => home_url(), 'title' => __('Trang chủ','chilitheme') ); array_push($breadcrumb,$breadcrumb_item); //at singular template if(is_singular() && !is_page()): global $post; //Term $taxonomy_names = get_object_taxonomies($post); //var_dump(get_post_type()); $term_arr=array(); if(is_singular('product')){ $taxonomy = 'product_cat'; }else{ $taxonomy = $taxonomy_names[0] ; } //var_dump($taxonomy); $terms = get_the_terms( $post->ID, $taxonomy ); //var_dump($terms[0]->term_id); if($terms): foreach ( $terms as $term ): //get parent term top $term_parent_arr=get_ancestors($term->term_id,$taxonomy); if($term_parent_arr): foreach($term_parent_arr as $item): $term_item = get_term($item,$taxonomy); 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); $breadcrumb_item=array( 'url' => get_term_link( (int) $term->term_id,$taxonomy), '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' => __('Tin tức') ); array_push($breadcrumb,$breadcrumb_item); endif; //at taxonomy template if((is_tax() || is_category())): global $wp_query; $term = $wp_query->get_queried_object(); $term_arr=array(); //echo "<pre>"; //print_r($wp_query->queried_object->taxonomy); //echo "</pre>"; //get parent term top //$taxonomy_names = get_object_taxonomies(array('post','product')); //var_dump($taxonomy_names); $term_parent_arr=get_ancestors($term->term_id,$wp_query->queried_object->taxonomy); if($term_parent_arr): foreach($term_parent_arr as $item): $term_item = get_term($item,$wp_query->queried_object->taxonomy); 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,$wp_query->queried_object->taxonomy); $breadcrumb_item=array( 'url' => get_term_link( (int) $term->term_id,$wp_query->queried_object->taxonomy), '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; //var_dump($breadcrumb); if($breadcrumb): ?> <div class="breadcrumb"> <?php $i=1; foreach($breadcrumb as $item):?> <?php if($i == 1){ ?> <span><a href="<?php echo $item['url']; ?>"><i class="fas fa-home"></i> Trang chủ</a></span> <?php }else{ ?> <span><a href="<?php echo $item['url']; ?>" title="<?php echo $item['title']; ?>"><?php echo $item['title']; ?></a></span> <?php } ?> <?php $i++; endforeach; ?> </div> <?php endif; ?> <?php } |
Cách dùng gọi breadcrumb();
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