您在這裡

為MediaWiki添加帶結構化數據的面包屑導航

James Qi 在 2018年12月16日 - 10:14 發表

  前些天寫了一篇《為網站面包屑導航添加結構化數據》,主要說的是為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站很要麻煩一些,目前隻在部分網站的部分模闆中實施了,後續再在更多地方實施。

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。