需要将url添加到php breadcrumb以显示它们所在的页面

时间:2015-02-26 23:27:40

标签: php code-snippets breadcrumbs

我从Google Rich片段收到错误,因为显示的页面的面包屑网址丢失了;我需要添加到此脚本以获取“最后”面包屑上的URL。感谢

    <p> <div id="breadcrumb" style="font-variant:small-caps;"><span>
<? 

$path = $_SERVER["PHP_SELF"]; 
$tree = split("/",$path); 
$breadcrumb =  '';
$totalbreadcrumb = '';
$divstring  = '<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">';
if (count($tree)==2 && $tree[1]=="/index.php") { // First, check if we are on the home page 
    $breadcrumb =  $divstring . "home"; 


} else { // If not, then first put a link to the homepage 
    $breadcrumb = "<a href=\"/index.php\">" . $divstring . "home </a>"; 
    $totalbreadcrumb = "home";

    for ($i=1; $i<count($tree); $i++) { // Now step through each level adding a link, until we reach an actual file 

        if (strstr($tree[$i],".")) { // Found a file (i.e. it has a fullstop character in it) 
            if ($tree[$i]!="/index.php") { // If it is not the index page of the current folder, print the name 
                $pagename = split("\.",$tree[$i]); 
                $breadcrumb = $breadcrumb . " &gt; " .  $divstring . str_replace("_"," ",$pagename[0]); 


            } 
        } else { // Found another directory, so provide a link to the top level... 
            if ($tree[$i+1]=="/index.php") { // ...unless the next one down is the index page 
                $breadcrumb = $breadcrumb . " &gt; "  . $divstring  . str_replace("_"," ",$tree[$i]); 


            } else { 
                $breadcrumb = $breadcrumb . " &gt; <a href=\""; // Add the arrow between nodes 
                for ($j=1; $j<=$i; $j++) { // Add the right link depth to the actual link 
                    $breadcrumb = $breadcrumb . "/" . $tree[$j]; 



                } 
                $breadcrumb = $breadcrumb . "/\">" .  $divstring . $tree[$i] . "</span></a>";                

            } 
        } 
    } 
} 

  echo  $breadcrumb;  // Print the final breadcrumb trail to the page 
?></div></span></p></div>

1 个答案:

答案 0 :(得分:0)

我终于发现Google结构化数据(丰富网页摘要)实际上要我添加itemprop =&#34; url&#34;并且只能使用    <span itemscope itemtype="http://data-vocabulary.org/Breadcrumb">一次。这是php rich snippets breadcrumbs的正确代码。

    <p> <div id="breadcrumb" style="font-variant:small-caps;"><span><span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">
<? 

$path = $_SERVER["PHP_SELF"]; 
$tree = split("/",$path); 
$breadcrumb =  '';
$totalbreadcrumb = '';
$divstring  = '<span itemprop="title">';
if (count($tree)==2 && $tree[1]=="/index.php") { // First, check if we are on the home page 
    $breadcrumb =  $divstring . "home"; 


} else { // If not, then first put a link to the homepage 
    $breadcrumb = "<a href=\"/index.php\" itemprop=\"url\">" . $divstring . "home </a>"; 
    $totalbreadcrumb = "home";

    for ($i=1; $i<count($tree); $i++) { // Now step through each level adding a link, until we reach an actual file 

        if (strstr($tree[$i],".")) { // Found a file (i.e. it has a fullstop character in it) 
            if ($tree[$i]!="/index.php" ) { // If it is not the index page of the current folder, print the name 
                $pagename = split("\.",$tree[$i]); 
                $breadcrumb = $breadcrumb . " &gt; " .  $divstring . str_replace("_"," ",$pagename[0]); 


            } 
        } else { // Found another directory, so provide a link to the top level... 
            if ($tree[$i+1]=="/index.php") { // ...unless the next one down is the index page 
           $breadcrumb = $breadcrumb . " </span> &gt; " . $divstring   . str_replace("_"," ",$tree[$i]); 


            } else { 
                $breadcrumb = $breadcrumb . " &gt; <a href=\""; // Add the arrow between nodes 
                for ($j=1; $j<=$i; $j++) { // Add the right link depth to the actual link 
                    $breadcrumb = $breadcrumb . "/" . $tree[$j]; 



                } 
               $divstring .   $breadcrumb = $breadcrumb . "/\" itemprop=\"url\">" . $tree[$i] . "</span></a>";                

            } 
        } 
    } 
} 

  echo  $breadcrumb;  // Print the final breadcrumb trail to the page 
?></div></span></p></div>