【问题标题】:WordPress post image to show not the featured imageWordPress 发布图片不显示特色图片
【发布时间】:2017-01-09 09:35:48
【问题描述】:
我在 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 代码,因为帖子中没有任何特色图片,但我已在内部帖子内容中添加了图片。我希望该帖子图片作为特色图片。
【问题讨论】:
标签:
php
wordpress
plugins
【解决方案1】:
把它放在你主题的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('')); 显示图像的 url “single.php 或 post.php”
<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>
如果找不到图片,请将“http://example.com/defaultimage.jpg”更改为默认图片网址。