关于PressWork增加导航面包屑的实现

很开心,周末为自己的博客更换了新装,没错,就是PressWork。它是一个自定义很强的开源wp模板项目,但是还是缺少很多实用的模块,这一部分还是需要童鞋们自己根据自己的需求去定制。

这次我先来介绍对于PressWork的导航面包屑(Breadcrumb)的定制实现。

一、由于默认样式的情况,作者决定在header中新增面包屑的option

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
<?php
/**
* Add breadcrumb to header option
* @author chenjinlong
* @description presswork/functions.php
*/
if(empty($pw_default_options)) {
$pw_default_options = array(
// 新增breadcrumb选项
"header_option" => "header_logo,nav,breadcrumb"
// 其他配置项目 …
);
}
```




二、新增面包屑实现函数

```php
<?php
/**
* Deal with breadcrumb for single post | page.
* @author chenjinlong
* @description presswork/functions.php
*/
function pw_get_breadcrumb(){
$home = '<a href="'.home_url().'">首页</a>' . ' » ';
$category_parent_top = '';
if(is_single()){
$categorys = get_the_category();
$category_parent_top = get_category_parents($categorys[0]->term_id, true, ' » ');
$title = the_title('', '', false);
}elseif(is_page()){
$title = the_title('', '', false);
}else{
$title = '';
}
return $home . $category_parent_top . $title;
}
```




三、增加元素输出逻辑分支

```php
<?php
/**
* Add page breadcrumb for single post top.
* @author chenjinlong
* @description presswork/functions.php, ‘pw_get_element’ function
*/
if($pw_add_name=="breadcrumb"){
echo '<li>';
if(is_single() || is_page()){
$handle = pw_get_breadcrumb();
echo $handle;
}
echo '</li>';
}

参照以上的步骤,简单版的面包屑导航就弄好了,如果需要提高视觉美感,再自己增加css样式的支持即可,因为每个人要求不一,这里就不作建议样式了。