【问题标题】:How to hide featured image if post have image on wordpress如果帖子在wordpress上有图片,如何隐藏特色图片
【发布时间】:2020-04-07 04:23:44
【问题描述】:

如果帖子内容中没有媒体,我想显示特色图片。我在下面尝试过,但它不起作用:

.has-post-thumbnail .entry-featured-media.entry-featured-media-main {
    display:none
}

【问题讨论】:

    标签: css wordpress image


    【解决方案1】:

    我怀疑它只能通过 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()))会检查帖子内容中是否没有图片。

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2015-09-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多