WordPress发布图片以显示不是特色图片

时间:2017-01-09 09:35:48

标签: php wordpress plugins

我在WordPress中有一个网站,我想显示WordPress发布的第一张图片,该图片显示在帖子中但不记得特色图片。所以如何在帖子中显示它,我希望我的帖子如下 this is how blog design should be

这是代码

<div class="col-md-3 small-post-img">
 <a href="<?php the_permalink() ?>">
  <?php the_post_thumbnail(); ?>
 </a>
</div>

它没有采用the_post_thumbnail代码,因为帖子中没有任何特色图片,但我在内部帖子内容中添加了图片。我想把那些帖子图片作为特色图片。

1 个答案:

答案 0 :(得分:0)

将它放在主题的functions.php文件

function ash_post_first_img($post_content){
    preg_match_all('/<img[^>]+>/i', $post_content, $content_all_images);
    preg_match_all('/src="([^"]+)"/i', $content_all_images[0][0], $content_image);
    if (isset($content_image[1][0])) {
        return $content_image[1][0];    
    } else{
        return 'http://example.com/defaultimage.jpg';
    }
}

并使用此功能ash_post_first_img(get_the_content(''));,您希望在其中显示图片&#34; single.php或post.php&#34;

<div class="col-md-3 small-post-img">
    <a href="<?php the_permalink() ?>">
        <img src="<?php echo ash_post_first_img(get_the_content('')); ?>">
    </a>
</div>

更改&#34; http://example.com/defaultimage.jpg&#34;如果没有找到图像,则返回默认图像网址。

相关问题