【发布时间】:2020-04-07 04:23:44
【问题描述】:
如果帖子内容中没有媒体,我想显示特色图片。我在下面尝试过,但它不起作用:
.has-post-thumbnail .entry-featured-media.entry-featured-media-main {
display:none
}
【问题讨论】:
如果帖子内容中没有媒体,我想显示特色图片。我在下面尝试过,但它不起作用:
.has-post-thumbnail .entry-featured-media.entry-featured-media-main {
display:none
}
【问题讨论】:
我怀疑它只能通过 css 完成。 找到this类似问题的答案。
试试这个:
在functions.php中添加:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
然后将以下内容放在你的post php中(single.php或者你需要的模板):
if (!catch_that_image()) {
if ( has_post_thumbnail()) {
the_post_thumbnail();
}
}
这个(if (!catch_that_image()))会检查帖子内容中是否没有图片。
【讨论】: