现在位置: 首页 > CMS知识 > 正文

wordpass 首页缩略图的调用方法

发布时间:2022 年 1 月 3 日

本文作者:帮建站

点击次数:645

wordpress模板中找到模板函数文件functions.php;
在functions.php粘贴下面的缩略图代码:

//调用缩略图
function get_first_image() {
global $post;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+ data-cke-saved-src=”[\'” src=”http://www.m-bj.com/[/'”>/i'</img.+>, $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = bloginfo(‘template_url’) . “/default.jpg”;
};
return $first_img;
}

在网站模板的任意一个模板中,只要需显示缩略图片位置,使用下面的调用代码来调用缩略图。

if ( has_post_thumbnail() ) { the_post_thumbnail(array(296,296),array(‘alt’=> trim(strip_tags( $post->post_title ))));} else {?><img src=”” alt=”” width=”296″ height=”296″/>}?>

上面代码里的29,296为图片的尺寸,可以根据自己网站的需要进行修改。
在主题文件夹下放一个默认图片,名字为default.jpg。
通过上面的方法,我们自己建网站时,也可以很轻松的调用文章缩略图片了。

问题本身还是很简单的,只是对于不熟悉的网友来说不知道什么原因,如果英语不太好的话,就以为自己买了个假主题了。问题本身还是很简单的,只是对于不熟悉的网友来说不知道什么原因,如果英语不太好的话,就以为自己买了个假主题了。


首页 在线 手机