Tìm theo từ khóa

Hàm tạo breadcrumb cho wordpress



Dùng hàm sau thêm vào function.php để tạo beadcrumb theo cấu trúc danh mục cha ,con cho wordpress nhé

function codfe_get_blog_breadcrumbs() {
	if (!is_home()) {
		$html ='';
		$html.= '<a href="';
		$html.= get_option('home');
		$html.= '"> Trang chủ</a> » ';
		if (is_category() || is_single()) {
			
			$cats=get_the_category();
			$cid=array();
			foreach($cats as $cat)  { $cid[]=$cat->cat_ID; }
			$cid=implode(',', $cid);
			
			foreach((get_categories('orderby=term_group&include='.$cid)) as $category) { // notice orderby
				$html.= '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a> » '; // keep a space after </a> as seperator 
			}
			
			$html = substr($html,0,-3);
			
			//the_category(' + ');
			if (is_single()) {
				//echo " » ";
				//the_title();
			}
		} elseif (is_page()) {
			$html.= the_title();
		}
		
		echo $html;
	}
}

Lưu ý: hảm get_categories(‘orderby=term_group&include=’.$cid) còn có thể sort theo các tiêu chí sau:

id 
name - default 
slug 
count 
term_group

Để hiển thị ra chúng ta có thể hook vào trong post của flatsome như sau (thêm vào function.php)

add_action( 'flatsome_before_blog' , 'codfe_get_blog_breadcrumbs', 20 );

hoặc tạo shortcode để hiển thị vị trí cần thiết (thêm vào function.php)

add_shortcode('breadrummenu', 'codfe_get_blog_breadcrumbs' );

Hiển thị shortcode bằng cách thêm  hoặc trong php thì dùng như sau:

echo do_shortcode('[breadrum-menu]');

Chúc các bạn thành công nhé!

5/5 - (1 bình chọn)
0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Mới nhất
Cũ nhất Được bỏ phiếu nhiều nhất
0
Rất thích suy nghĩ của bạn, hãy bình luận.x