简单的面包屑问题

时间:2013-01-13 19:12:12

标签: php

我一直想弄清楚如何让我的痕迹中的最后一项有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>';
}

对此有何帮助?

1 个答案:

答案 0 :(得分:4)

试试这个:

$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>';
}