首先在functions.php添加以下代码:
/**
*判断文章中是否有图片
*/
function is_has_image(){
if ( is_single () || is_page()) {
global $post;
if( has_post_thumbnail() ) return true;
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
if(!empty($strResult[1])) return true;
return false;
}
}
在需要的地方进行调用:
<?php if( is_has_image() ) :?>
......//功能代码
<?php endif;?>