dongyuli4538 2014-05-21 20:48
浏览 12

修改php只在元描述字段为空并且拉2个句子时运行(Magento)

I have the following PHP code in Magento which so far takes the first sentence of the description and makes it the meta description.

<?php if (Mage::registry('current_product')) : ?>
<?php if (strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1)))=="") : ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1))).'" />' ?>
<?php endif; ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php endif; ?>

I'm trying to do two things, modify it so that it only runs if the product is missing a manually set meta description, and make it pull the first 2 sentences instead of the first 1.

Impossible?

  • 写回答

1条回答 默认 最新

  • duanfoumi5620 2014-05-21 21:31
    关注

    I'm trying to do two things, modify it so that it only runs if the product is missing a manually set meta description, and make it pull the first 2 sentences instead of the first 1. Impossible?

    Yes, it's impossible. Because, if the product description is empty you simply can't pull two sentences from it. Not even one ;)

    Notes:

    • you don't need PHP tags on every line
    • your if-syntax works, but is not common
    • fetch the description only once, not 4 times from the registry
    • compare with empty, to test if a description is set
    • if not set, print "No Description."
    • if set, get two sentences
    • to fetch two sentences, i added the function teaser() it's from somewhere on SO

    --

    <?php
    if (Mage::registry('current_product'))
    {
        $metadescription = $this->description();        
    
        if ($metadescription === '') {
            $description = Mage::registry('current_product')->getDescription();
            $twoSentences = teaser($description, 2);
    
            echo '<meta name="description" content="'.htmlspecialchars($twoSentences).'" />';
        }
    }
    
    function teaser($body, $sentencesToDisplay = 2)
    {
        $nakedBody = preg_replace('/\s+/',' ',strip_tags($body));
        $sentences = preg_split('/(\.|\?|\!)(\s)/',$nakedBody);
    
        if (count($sentences) <= $sentencesToDisplay)
            return $nakedBody;
    
        $stopAt = 0;
        foreach ($sentences as $i => $sentence) {
            $stopAt += strlen($sentence);
    
            if ($i >= $sentencesToDisplay - 1)
                break;
        }
    
        $stopAt += ($sentencesToDisplay * 2);
    
        return trim(substr($nakedBody, 0, $stopAt));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)