PHP面包屑数组,链接不完整

问题描述 投票:0回答:1

我将以下代码用于面包屑:

<?php

class Breadcrumb
{
   private $breadcrumb;
   private $separator = ' / ';
   private $domain = 'example.org';

   public function build($array)
   {
      $breadcrumbs = array_merge(array('Home' => ''), $array);
      $count = 0;
      foreach($breadcrumbs as $title => $link) {
         $this->breadcrumb .= '
         <span itemscope="" itemtype="https://schema.org/BreadcrumbList">
            <a href="'.$this->domain. '/'.$link.'" itemprop="url">
               <span itemprop="title">'.$title.'</span>
            </a>
         </span>';

         $count++;
         if($count !== count($breadcrumbs)) {
            $this->breadcrumb .= $this->separator;
         }
      }
      return $this->breadcrumb;
   }
}

 ?>

我称它为:

<?php
$breadcrumb = new Breadcrumb();
  echo $breadcrumb->build(array(
  $pageTitle => 'about',
    'More' => 'more.php'
  )); 
?>

pageTitle是每页顶部的var。

输出正确,并显示:主页/关于/更多但是,它们每个上的链接如下:

Home: example.org
About: example.org/about
More: example.org/more.php

而且我正在寻找这样的输出:example.org/about/more.php

非常感谢!

php breadcrumbs
1个回答
0
投票

您可以在循环中连接链接...

$bclink = '';
foreach($breadcrumbs as $title => $link) {
    if ($link != '') {
        $bclink .= '/' . $link;
    }
    $this->breadcrumb .= '
        <span itemscope="" itemtype="https://schema.org/BreadcrumbList">
            <a href="'.$this->domain.$bclink.'" itemprop="url">
                <span itemprop="title">'.$title.'</span>
            </a>
        </span>';

    $count++;
    if($count !== count($breadcrumbs)) {
        $this->breadcrumb .= $this->separator;
    }
}
热门问题
推荐问题
最新问题