修剪Magento的面包屑

时间:2011-01-19 00:50:41

标签: php javascript magento

我需要在整个网站上限制Magento面包屑的输出,我认为我在这个脚本上的大部分方式。

    $breadcrumbs = "";
    $bleng = 0;
    foreach($crumbs as $_crumbName=>$_crumbInfo):
        $breadcrumbs .= "<li class=\"".$_crumbName."\">";
        $bleng = $bleng + strlen($_crumbName);
        if($_crumbInfo['link']):
            $breadcrumbs .= "<a href=\"".$_crumbInfo['link']."\" title=\"".$this->htmlEscape($_crumbInfo['title'])."\">".$this->htmlEscape($_crumbInfo['label'])."</a>";
            $bleng = $bleng + strlen($this->htmlEscape($_crumbInfo['label']));
        elseif($_crumbInfo['last']):
            $breadcrumbs .= "<strong>".$this->htmlEscape($_crumbInfo['label'])."</strong>";
            $bleng = $bleng + strlen($this->htmlEscape($_crumbInfo['label']));
        else:
            $breadcrumbs .= $this->htmlEscape($_crumbInfo['label']);
            $bleng = $bleng + strlen($this->htmlEscape($_crumbInfo['label']));
       endif;
       if(!$_crumbInfo['last']):
           $breadcrumbs .= "<span>&gt;</span>";
       endif;
       $breadcrumbs .= "</li>";
    endforeach;

    if ($bleng > 70): // Arbitrary threshold
        $trimmed = $breadcrumbs;
        echo substr($trimmed, 0, 70);
        echo $trimmed."...";
    else:
        echo $breadcrumbs;
    endif;

这个快速修改的问题是$bleng正在计算html标记和可见文本中的字符。我需要的是只计算可见文本(面包屑'标签')并相应地修剪$breadcrumbs。实际上,这个脚本正在修剪任何可见文本顶部的字符串和标签,但最后添加它们似乎不起作用:

echo $trimmed."...</a></li>";

我怀疑有一种SEO友好的JS方法可以做到这一点,不需要我使用任意字符限制。我很乐意为此使用PHP或JS,只要它有效。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

<强>解

我意识到我所做的事情是倒退的。我修改了脚本以从我的任意数量的允许字符开始倒计时,当它到达最终的“crumb”时,将显示文本修剪为$bleng(此时为负数)。

这适用于我的商店,因为不超过3个子类别级别。

    <?php
    $breadcrumbs = "";
    $bleng = 70;
    foreach($crumbs as $_crumbName=>$_crumbInfo):
        $breadcrumbs .= "<li class=\"".$_crumbName."\">";
        if($_crumbInfo['link']):
            $breadcrumbs .= "<a href=\"".$_crumbInfo['link']."\" title=\"".$this->htmlEscape($_crumbInfo['title'])."\">".$this->htmlEscape($_crumbInfo['label'])."</a>";
            $bleng = $bleng - strlen($this->htmlEscape($_crumbInfo['label']));
            $bleng - 3;
        elseif($_crumbInfo['last']):
            $lastleng = strlen($this->htmlEscape($_crumbInfo['label']));
            $lastlabel = $this->htmlEscape($_crumbInfo['label']);
            $bleng = $bleng - $lastleng;
            if ($bleng < 0):
                $breadcrumbs .= "<strong>".substr($lastlabel, 0, $bleng)."...</strong>";
            else:
                $breadcrumbs .= "<strong>".$lastlabel."</strong>";
            endif;
        else:
            $breadcrumbs .= $this->htmlEscape($_crumbInfo['label']);
            $bleng = $bleng - strlen($this->htmlEscape($_crumbInfo['label']));
            $bleng - 3;
        endif;
         if(!$_crumbInfo['last']):
               $breadcrumbs .= "<span>&gt;</span>";
         endif;
        $breadcrumbs .= "</li>";
    endforeach;
    echo $breadcrumbs;
    ?>