I wanted to add a classified breadcrumb path to the project, but the oneThink frontend didn't seem to provide such a function, so I wrote one myself. I hope it will be useful to everyone.
When we build a website, we often use breadcrumb navigation. Generally, this kind of navigation is based on multi-level classification, and then pushed up level by level. In oneThink, whether it is a list page or an article page, The variable of the current category $category is defined. Therefore, we can use the id in this variable to generate the current path by using the function call of the front-end module.
Without further ado, let’s first create a function in the function of the HOME module, get_category_info();, which is used to find the content of the specified field of the category. It is intended to query the pid and allow_publish fields/**Find the content of the specified field in the specified category<br>
* @param $id<br>
* @param string $field<br>
* @return mixed|string<br>
*/<br>
function get_category_info($id,$field=""){<br>
<br>
If(!$field){<br>
return 'No search field specified';<br>
}else{<br>
$map = array(<br>
'id'=>array('eq',$id),<br>
'status'=>array('eq',1)<br>
);<br>
$re = M('category')->where($map)->getField($field);<br>
Return $ Re; <br>
}<br>
}
Then create the get_bread() function/**<br>
* Get breadcrumbs <br>
* Generate classified breadcrumbs based on classification<br>
* @param $id Current category ID<br>
*/<br>
function get_bread($id){<br>
//Query pid<br>
$pid = get_category_info($id,'pid');<br>
//Determine whether to use the index template or the lists template based on whether the current category allows publishing content<br>
$temp = get_category_info($id,'allow_publish')?'lists':'index';<br>
<br>
$str = $pid?<br>
get_bread($pid):<br>
'<a href="'.U('Home/Index/index'). '" alt="Home" >Home</a>';<br>
<br>
$str .= ' > <a href="'.U('Home/Article/'.$temp,array('category'=>$id)).'" alt="'.get_category_title($ id).'">'.get_category_title($id).'</a>';<br>
<br>
Return $str;<br>
}
Finally, the front-end tag calls the function get_bread(){:get_bread($category['id'])}