前些天写了一篇《为网站面包屑导航添加结构化数据》,主要说的是为Drupal网站的面包屑导航添加结构化数据标签,从实际应用来看是可以起到预期效果的。
后来我又尝试了在MediaWiki网站中也为面包屑导航添加结构化数据标签,做起来麻烦一些但依然是可行的,今天来记录一下。
我们从2006年很早就开始使用MediaWiki软件了,尝试了用模板来做一个面包屑导航,代码类似这样:
<div class="breadcrumb">
[[link:|首页]]
>
[[link:{{#var:province}}|{{#var:province}}]]
>
[[link:{{#var:province-city}}|{{#var:city}}]]
>
[[link:{{#var:province-city-county}}|{{#var:province-city-county}}]]
>
[[link:{{#var:postcode}}|邮编{{#var:postcode}}]]
</div>
这里面用到了一些MediaWiki的变量扩展程序,在邮编库中的一个例子页面中显示出来的效果类似这样:
首页 > 广东省 > 深圳市 > 广东省深圳市宝安区 > 邮编518109
可以逐级导航,但以前并没有结构化数据。
这次修改了模板,代码如下:
{{#switch:{{SERVERNAME}}
|www.youbianku.com
|cn.m.youbianku.com
|cn.amp.youbianku.com
|tw.youbianku.com
|tw.m.youbianku.com
|tw.amp.youbianku.com=
<html><div class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"></html>
{{#vardefine:count|1}}<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:首页}}<html>">
<span itemprop="name">首页</span></a>
<meta itemprop="position" content="</html>{{#var:count}}<html>" />
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province}}}}<html>">
<span itemprop="name"></html>{{#var:province}}<html></span></a>
<meta itemprop="position" content="</html>{{#var:count}}<html>" />
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province-city}}}}<html>">
<span itemprop="name"></html>{{#var:city}}<html></span></a>
<meta itemprop="position" content="</html>{{#var:count}}<html>" />
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province-city-county}}}}<html>">
<span itemprop="name"></html>{{#var:province-city-county}}<html></span></a>
<meta itemprop="position" content="</html>{{#var:count}}<html>" />
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:postcode}}}}<html>">
<span itemprop="name"></html>邮编{{#var:postcode}}<html></span></a>
<meta itemprop="position" content="</html>{{#var:count}}<html>" />
</span>
</html>
<html></div></html>
|cn.mip.youbianku.com
|tw.mip.youbianku.com=
<html><div class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"></html>
{{#vardefine:count|1}}<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:首页}}<html>">
<span itemprop="name">首页</span></a>
<span itemprop="position" content="</html>{{#var:count}}<html>"></span>
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province}}}}<html>">
<span itemprop="name"></html>{{#var:province}}<html></span></a>
<span itemprop="position" content="</html>{{#var:count}}<html>"></span>
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province-city}}}}<html>">
<span itemprop="name"></html>{{#var:city}}<html></span></a>
<span itemprop="position" content="</html>{{#var:count}}<html>"></span>
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:province-city-county}}}}<html>">
<span itemprop="name"></html>{{#var:province-city-county}}<html></span></a>
<span itemprop="position" content="</html>{{#var:count}}<html>"></span>
</span>
</html>{{#vardefine:count|{{#expr:{{#var:count}}+1}}}}
»
<html>
<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="</html>/{{urlencode:{{#var:postcode}}}}<html>">
<span itemprop="name"></html>邮编{{#var:postcode}}<html></span></a>
<span itemprop="position" content="</html>{{#var:count}}<html>"></span>
</span>
</html>
<html></div></html>
}}
这里面首先判断了是否MIP版本,对MIP版本做了点特殊处理。然后添加了面包屑导航需要的结构化数据,其中为了计数还加入了一个变量count的计算,显得复杂一些,需要对MediaWiki有一定编辑经验的人才容易看懂。
这个做起来比Drupal站很要麻烦一些,目前只在部分网站的部分模板中实施了,后续再在更多地方实施。
评论