开发者

Trimming Magento's breadcrumbs

I need to limit the output of Magento's breadcrumbs across my entire site, and I think I am most of the way there on this script.

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

The problem with this quick modification is that $bleng is counting characters in the html markup as well as in the visible text. What I need is to count only the visible text (breadcrumb 'labels') and trim $breadcrumbs accordingly. As it is, this script is trimming the and tags from the string on top of any visible text, but adding t开发者_JS百科hem back in at the end doesn't seem to work using:

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

I suspect there is an SEO-friendlier JS way to do this that doesn't require that I use an arbitrary character limit. I'm happy to use either PHP or JS for this, so long as it works. Any suggestions?


SOLUTION

I realized that what I was doing was backwards. I have revised the script to count down from my arbitrary number of permitted characters, and when it gets to the final "crumb", trims the display text by $bleng (a negative number at this point).

This works for my store since there are no more than 3 sub-category levels.

    <?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;
    ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜