I found various solutions for generating breadcrumbs, but all of them use elements, but my current breadcrumb is in unordered list style.
我发现了各种用于生成面包屑的解决方案,但它们都使用了元素,但我目前的面包屑是无序的列表样式。
Here is what I done so far:
这是我到目前为止所做的:
<div class="breadcrumb">
<ul>
<?php $seocrumbs = array_pop($breadcrumbs); ?>
<?php foreach($breadcrumbs as $idx=>$seocrumb) { ?>
<li>
<div <?php echo $idx == 0 ? '' : 'itemprop="child"'; ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="<?php echo $seocrumb['url'];?>" itemprop="url">
<span itemprop="title"><?php echo $seocrumb['text'];?></span>
</a>
</div>
</li>
<?php }?>
<?php
//remove link from last item
echo $seocrumb['separator'];?> <li id="lastitem"><?php echo $seocrumbs['text']; ?></li>
</ul>
</div>
The problem with this code is that it is generating separate breadcrumbs, instead of mother key and childs.
此代码的问题在于它生成单独的面包屑,而不是母键和子项。
<div class="breadcrumb">
<ul>
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="" itemprop="url">
<span itemprop="title">Index</span>
</a>
</div>
</li>
<li>
<div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="" itemprop="url">
<span itemprop="title">Category2</span>
</a>
</div>
</li>
<li id="lastitem">Plášte 27.5"</li>
</ul>
</div>
2 个解决方案
#1
2
Assuming that you have some sort of test breadcrumb arrays, similar to this:
假设你有一些测试面包屑数组,类似于:
$array1 = array ( "url"=>"http://example.com", "text"=>"Parent","separator"=>" ");
$array2 = array ( "url"=>"http://example.com", "text"=>"Child","separator"=>" ");
$breadcrumbs = array ($array1,$array2);
I modified your code as follows:
我修改了你的代码如下:
$output = "<div class='breadcrumb'><ul><li>";
$itemType = "itemtype='http://data-vocabulary.org/Breadcrumb'";
foreach ($breadcrumbs as $key=>$value) {
if ($key != 0) {
$itemProp = "itemprop='child'";
$output .= "<li>";
} else {
$itemProp = '';
}
$output .= "<div $itemProp itemscope $itemType>";
$output .= " <a href='{$value['url']}' itemprop='url'>";
$output .= " <span itemprop='title'>";
$output .= " {$value['text']}";
$output .= " </span>";
$output .= " </a>";
$output .= "</div>";
if ($key != 0) {
$output .= "</li>";
} else {
$output .= "<ul>";
}
}
$output .= '</ul></li></ul></div>';
echo $output;
Which gives
<div class='breadcrumb'>
<ul>
<li>
<div itemscope itemtype='http://data-vocabulary.org/Breadcrumb'>
<a href='http://example.com' itemprop='url'>
<span itemprop='title'>Parent</span>
</a>
</div>
<ul>
<li>
<div itemprop='child' itemscope itemtype='http://data-vocabulary.org/Breadcrumb'>
<a href='http://example.com' itemprop='url'>
<span itemprop='title'>Child</span>
</a>
</div>
</li>
</ul>
</li>
</ul>
</div>
I'm making some assumptions about your starting array formatting, but this should be a workable format for you to tweak it and meet your needs.
我对你的起始数组格式做了一些假设,但这应该是一个可行的格式,你可以调整它并满足你的需求。
#2
1
A foreach loop will generate a 1 dimensional breadcrumb trail. If you want
foreach循环将生成1维痕迹痕迹。如果你想
<ul>
<li>Category 1
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</li>
<li>Category 2</li>
</ul>
You will need 2 foreach loops, 1 for each category, and 1 for each key.
您将需要2个foreach循环,每个类别1个,每个键1个。
<ul>
<?php
foreach ($categories as $c_idx=>$c_value){
echo "<li>$c_value<ul>";
foreach ($items as $i_idx=>$i_value){
echo "<li>$i_value</li>"
}
echo "</ul>";
echo "</li>"
}
?>
</ul>
#1
2
Assuming that you have some sort of test breadcrumb arrays, similar to this:
假设你有一些测试面包屑数组,类似于:
$array1 = array ( "url"=>"http://example.com", "text"=>"Parent","separator"=>" ");
$array2 = array ( "url"=>"http://example.com", "text"=>"Child","separator"=>" ");
$breadcrumbs = array ($array1,$array2);
I modified your code as follows:
我修改了你的代码如下:
$output = "<div class='breadcrumb'><ul><li>";
$itemType = "itemtype='http://data-vocabulary.org/Breadcrumb'";
foreach ($breadcrumbs as $key=>$value) {
if ($key != 0) {
$itemProp = "itemprop='child'";
$output .= "<li>";
} else {
$itemProp = '';
}
$output .= "<div $itemProp itemscope $itemType>";
$output .= " <a href='{$value['url']}' itemprop='url'>";
$output .= " <span itemprop='title'>";
$output .= " {$value['text']}";
$output .= " </span>";
$output .= " </a>";
$output .= "</div>";
if ($key != 0) {
$output .= "</li>";
} else {
$output .= "<ul>";
}
}
$output .= '</ul></li></ul></div>';
echo $output;
Which gives
<div class='breadcrumb'>
<ul>
<li>
<div itemscope itemtype='http://data-vocabulary.org/Breadcrumb'>
<a href='http://example.com' itemprop='url'>
<span itemprop='title'>Parent</span>
</a>
</div>
<ul>
<li>
<div itemprop='child' itemscope itemtype='http://data-vocabulary.org/Breadcrumb'>
<a href='http://example.com' itemprop='url'>
<span itemprop='title'>Child</span>
</a>
</div>
</li>
</ul>
</li>
</ul>
</div>
I'm making some assumptions about your starting array formatting, but this should be a workable format for you to tweak it and meet your needs.
我对你的起始数组格式做了一些假设,但这应该是一个可行的格式,你可以调整它并满足你的需求。
#2
1
A foreach loop will generate a 1 dimensional breadcrumb trail. If you want
foreach循环将生成1维痕迹痕迹。如果你想
<ul>
<li>Category 1
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</li>
<li>Category 2</li>
</ul>
You will need 2 foreach loops, 1 for each category, and 1 for each key.
您将需要2个foreach循环,每个类别1个,每个键1个。
<ul>
<?php
foreach ($categories as $c_idx=>$c_value){
echo "<li>$c_value<ul>";
foreach ($items as $i_idx=>$i_value){
echo "<li>$i_value</li>"
}
echo "</ul>";
echo "</li>"
}
?>
</ul>