wordpress breadcrumbs最后一个元素不在标签中

时间:2016-11-15 13:25:36

标签: php wordpress

我正在尝试制作面包屑,但我不明白为什么最后一个元素(url - 我们在哪里)不在标签中,我该如何修复它。我需要最后一个元素是我的标签 - 。如果你帮助我,我将不胜感激。

PHP:

function breadcrumbs($separator = ' <i class="fa fa-angle-right fa-1g" aria-hidden="true"></i> ', $home = 'Main_page') {

    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    $breadcrumbs = array("<a href=\"$base_url\">$home</a>");

    $last = end( array_keys($path) );

    foreach( $path as $x => $crumb ){
        $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
        if( $x != $last ){
            $breadcrumbs[] = '<a href="'.$base_url.$crumb.'">'.$title.'</a>';
        }
        else {
            $breadcrumbs[] = $title;
        }
    }

    return implode( $separator, $breadcrumbs );
}

1 个答案:

答案 0 :(得分:1)

您应该删除最后一项检查以添加链接。这是修改后的代码:

function breadcrumbs($separator = ' <i class="fa fa-angle-right fa-1g" aria-hidden="true"></i> ', $home = 'Main_page') {

    $path        = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base_url    = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    $breadcrumbs = array("<a href=\"$base_url\">$home</a>");

    foreach( $path as $x => $crumb ){
        $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));

        $breadcrumbs[] = '<a href="'.$base_url.$crumb.'">'.$title.'</a>';
    }

    return implode( $separator, $breadcrumbs );
}