【问题标题】:Joomla 3. Set first image of article as intro image [duplicate]Joomla 3.将文章的第一张图片设置为介绍图片[重复]
【发布时间】:2020-03-25 06:28:52
【问题描述】:

7mo嗨, 我有 joomla 3,我想将文章的第一张图片设置为类别博客覆盖中的介绍图片。 我有一个插件可以抓取文章的第一张图片并将其设置在 og 图像元标记中。我认为我们可以使用这个功能来解决我的问题。 这是插件的代码

// Image
     $pictures = '';
     if (isset($row->images)) {
        //$pictures = json_decode($row->images);
        $pictures = (is_string($row->images) ? json_decode($row->images) : $row->images);
     }






     $imgSet = 0;


     if ($this->params->get('image'.$suffix, '') != '' && $parameterImage == 1) {
        $this->renderTag('og:image', $this->setImage($this->params->get('image'.$suffix, '')), $type);
        $imgSet = 1;
     } else if ($thisImg != ''){
        $this->renderTag('og:image', $this->setImage($thisImg), $type);
        $imgSet = 1;
     } else if (isset($pictures->{'image_intro'}) && $pictures->{'image_intro'} != '') {
        $this->renderTag('og:image', $this->setImage($pictures->{'image_intro'}), $type);
        $imgSet = 1;
     } else if (isset($pictures->{'image_fulltext'}) && $pictures->{'image_fulltext'} != '') {
        $this->renderTag('og:image', $this->setImage($pictures->{'image_fulltext'}), $type);
        $imgSet = 1;
     } else {
        // Try to find image in article


        $fulltext = '';
        if (isset($row->fulltext) && $row->fulltext != '') {
           $fulltext = $row->fulltext;
        }
        $introtext = '';
        if (isset($row->introtext) && $row->introtext != '') {
           $introtext = $row->introtext;
        }
        $content = $introtext . $fulltext;
        preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $src);
        if (isset($src[1]) && $src[1] != '') {
           $this->renderTag('og:image', $this->setImage($src[1]), $type);
           //$this->renderTag('og:image', JURI::base(false).$src[1], $type);
           $imgSet = 1;
        }


        // Try to find image in images/phocaopengraph folder
        if ($imgSet == 0) {
           if (isset($row->id) && (int)$row->id > 0) {


              jimport( 'joomla.filesystem.file' );
              $imgPath   = '';
              $path       = JPATH_ROOT . '/images/phocaopengraph/';
              if (JFile::exists($path . '/' . (int)$row->id.'.jpg')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.jpg';
              } else if (JFile::exists($path . '/' . (int)$row->id.'.png')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.png';
              } else if (JFile::exists($path . '/' . (int)$row->id.'.gif')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.gif';
              }


              if ($imgPath != '') {
                 $this->renderTag('og:image', $this->setImage($imgPath), $type);
                 $imgSet = 1;
              }
           }
        }
     }


     // If still image not set and parameter Image is set as last, then try to add the parameter image
     if ($imgSet == 0 && $this->params->get('image'.$suffix, '') != '' && $parameterImage == 0) {
        $this->renderTag('og:image', $this->setImage($this->params->get('image'.$suffix, '')), $type);
     }


     // END IMAGE

我想在我的覆盖页面中插入结果,此时

   <div class="image-intro">
      <?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
    <div>

但我不能为我的目的修改代码。 在覆盖 eof 模板中,我还找到了 $this-&gt;item-&gt;event-&gt;beforeDisolayContent; 认为显示全文。 我可以抓取文章的第一张图片并插入介绍文本吗?

【问题讨论】:

  • $pictures-&gt;{'image_intro'} 更简单地写成$pictures-&gt;image_intro。检查变量isset() 和non-falsey 时,最简单的方法是使用!empty()...isset($pictures-&gt;{'image_intro'}) &amp;&amp; $pictures-&gt;{'image_intro'} != '' 变为!empty($pictures-&gt;image_intro)

标签: php joomla


【解决方案1】:
$texthtml=$this->item->text; 
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
 $imgkk=$image['src'];
 echo '<img src="' . $imgkk. ' ">'
?>

我已经解决了!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
相关资源
最近更新 更多