简单的面包屑问题


Simple Breadcrumb Issue

我一直在试图弄清楚如何使最后一项在我的面包屑有H1标签。下面是一个没有H1的示例代码。

// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
    echo '
        <li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';
    // Show something before the link?
    if (isset($tree['extra_before']))
        echo $tree['extra_before'];
    // Show the link, including a URL if it should have one.
    echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
            <a href="' . $tree['url'] . '">' . $tree['name'] . '
            </a>' : '' . $tree['name'] . '';
    // Show something after the link...?
    if (isset($tree['extra_after']))
        echo $tree['extra_after'];
    echo '
         </li>';

我不是PHPer,我试着用这行代码做实验:

 <li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';

因为它只在面包屑的最后一项上显示"last"

我在下面尝试了一下,但显然它不起作用:

// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
    echo '
        <li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"><h1>' : '', '>';
    // Show something before the link?
    if (isset($tree['extra_before']))
        echo $tree['extra_before'];
    // Show the link, including a URL if it should have one.
    echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
            <a href="' . $tree['url'] . '">' . $tree['name'] . '
                <div id="arrow">
                  <div class="inside"></div>
                  <div class="border"></div>
               </div>
            </a>' : '' . $tree['name'] . '';
    // Show something after the link...?
    if (isset($tree['extra_after']))
        echo $tree['extra_after'];
    // Don't show a separator for the last one.
    if ($link_num != count($context['lateral_navigation']) - 1)
        echo '';
    echo '
        </h1', ($link_num == count($context['lateral_navigation']) - 1) , '></li>';
}

有什么帮助吗?

试试这个:

$count = count($context['lateral_navigation']); // get count
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
    $count--; // decrement by 1
    echo ($count == 0) // if zero (last)
        ? '<li class="last"><h1>'
        : '<li>';
    // Show something before the link?
    if (isset($tree['extra_before']))
        echo $tree['extra_before'];
    // Show the link, including a URL if it should have one.
    echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
            <a href="' . $tree['url'] . '">' . $tree['name'] . '
                <div id="arrow">
                  <div class="inside"></div>
                  <div class="border"></div>
               </div>
            </a>' : '' . $tree['name'] . '';
    // Show something after the link...?
    if (isset($tree['extra_after']))
        echo $tree['extra_after'];
    // Don't show a separator for the last one.
    if ($link_num != count($context['lateral_navigation']) - 1)
        echo '';
    echo ($count == 0) // if zero (last)
        ? '</h1></li>'
        : '</li>';
}