显示缩略图时Wordpress RSS提要问题

时间:2018-07-03 06:46:38

标签: php wordpress rss

以下代码未显示帖子的缩略图:

<?php
//grabs our post thumbnail image
    function get_first_image_url($html) {
        if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
            return $matches[1];
        }
    }
?>

以下代码未显示帖子的缩略图:

<span class="rss-image">
    <?php echo '<img src="' . get_first_image_url($item -> get_content()) . '"/>'; ?>
</span>

1 个答案:

答案 0 :(得分:1)

将其添加到functions.php

function get_first_image_url($fetch_image) {
      global $fetch_image, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $fetch_image, $matches);
      $first_img = $matches [1] [0];

      if(empty($first_img)){ //Defines a default image
        $first_img = "/images/default.jpg";
      }
      return $first_img;
    }

并使用此

<span class="rss-image">
    <?php echo '<img src="' . get_first_image_url($item -> get_content()) . '"/>'; ?>
</span>
相关问题