Last active
December 10, 2017 15:46
-
-
Save tai-sho/216bf744377be2dcbc93 to your computer and use it in GitHub Desktop.
SEO対策。microdataを付与したパンくずリストを生成する。HtmlHelper::addCrumbListを継承。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('HtmlHelper', 'View/Helper'); | |
class MyHtmlHelper extends HtmlHelper { | |
/** | |
* microdata属性対応版 | |
* @link https://support.google.com/webmasters/answer/185417?hl=en | |
* @see HtmlHelper::getCrumbList() | |
*/ | |
public function getCrumbList($options = array(), $startText = false) { | |
$defaults = array('firstClass' => 'first', 'lastClass' => 'last', 'separator' => '', 'escape' => true); | |
$options = (array)$options + $defaults; | |
$firstClass = $options['firstClass']; | |
$lastClass = $options['lastClass']; | |
$separator = $options['separator']; | |
$escape = $options['escape']; | |
unset($options['firstClass'], $options['lastClass'], $options['separator'], $options['escape']); | |
$crumbs = $this->_prepareCrumbs($startText, $escape); | |
if (empty($crumbs)) { | |
return null; | |
} | |
$result = ''; | |
$crumbCount = count($crumbs); | |
$ulOptions = $options; | |
$spanOptions = array('itemprop' => 'title', 'escape' => $escape); | |
foreach ($crumbs as $which => $crumb) { | |
$options = array(); | |
if (empty($crumb[1])) { | |
$elementContent = $crumb[0]; | |
} else { | |
$crumb[0] = $this->tag('span', $crumb[0], $spanOptions); | |
$crumb[2]['itemprop'] = 'url'; | |
$crumb[2]['escape'] = false; | |
$elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]); | |
$options['itemscope'] = ''; | |
$options['itemtype'] = 'http://data-vocabulary.org/Breadcrumb'; | |
} | |
if (!$which && $firstClass !== false) { | |
$options['class'] = $firstClass; | |
} elseif ($which == $crumbCount - 1 && $lastClass !== false) { | |
$options['class'] = $lastClass; | |
} | |
if (!empty($separator) && ($crumbCount - $which >= 2)) { | |
$elementContent .= $separator; | |
} | |
$result .= $this->tag('li', $elementContent, $options); | |
} | |
return $this->tag('ul', $result, $ulOptions); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment