缩略图要求是,如果文章有图就显示第一张图,如果没图就不显示。

修改wp-content/themes/主题名称/functions.php,加入一个函数,用来返回图片的url

function junzibuqi_get_post_img_url($thumbnail = true) {
global $post;
if (has_post_thumbnail ()) {
$domsxe = simplexml_load_string ( get_the_post_thumbnail () );
$thumbnailsrc = $domsxe->attributes()->src;
return $thumbnailsrc;
}elseif ($thumbnail) {
$content = $post->post_content;
preg_match_all ( ‘/<img.*?(?: |\\t|\\r|\\n)?src=[\'”]?(.+?)[\'”]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim’, $content, $strResult, PREG_PATTERN_ORDER );
$n = count ( $strResult [1] );
if ($n > 0) {
return $strResult [1] [0] ;
} else {
return trailingslashit( get_template_directory_uri() ) . ‘images/thumbnail.png’;
}
}else {

}
}

修改wp-includes/formatting.php,查找这一行$excerpt_more = apply_filters( ‘excerpt_more’, ‘ ‘ . ‘[&hellip;]’ );

将后面}之前的内容修改为

$text = wp_trim_words( $text , 150 );

if(substr(junzibuqi_get_post_img_url(true),-13,13)<>”thumbnail.png” )

if(is_home() or is_category() or is_archive() or is_search() )
// 直接输出
echo ‘<img src=”‘ . substr(junzibuqi_get_post_img_url(true),26,100) . ‘” alt=”‘ . get_the_title() . ‘” height=”100″ width=”100″ align=”left” hspace=”8″ vspace=”8″/>’;

 

$text = wp_trim_words( $text , 150 );里面的150是摘要字数,可以修改。

junzibuqi_get_post_img_url(true)的地址是绝对路径https://开头的,比如 img src=”https://retire50blog.wang/wp-content/uploads/2019/01/2019-01-11_145713.png”

http会导致chrome提示连接不安全,必须用https。

substr(junzibuqi_get_post_img_url(true),26,200)就是把域名这部分截掉,<img src会自动补上https的域名。变成 img src=”https://retire50blog.wang/wp-content/uploads/2019/01/2019-01-11_145713.png”

junzibuqi_get_post_img_url这个函数是百度来的,我也搞不明白他怎么写的,反正改一下能用就行了。

 

 

 

说点什么