国内外主机
测评及优惠码

WordPress后台文章如何调用缩略图?

  很多时候需要在网站上显示文章缩略图,这样可以让整个网站不是太单调。那么WordPress后台文章如何调用缩略图?

  我们自己做网站时,如何在自己网站上调用文章缩略图呢?下面来讲一下文章缩略图调用方法。

WordPress后台文章如何调用缩略图?

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

  //调用缩略图

  function get_first_image() {

  global $post;

  $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 = bloginfo(‘template_url’) . “/images/default.jpg”;

  };

  return $first_img;

  }

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

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

  上面代码里的29,296为图片的尺寸,可以根据自己网站的需要进行修改。

  通过上面的调用方法,我们自己建WordPress网站时,也可以很轻松的调用文章缩略图片了。

赞一个
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《WordPress后台文章如何调用缩略图?》
文章链接:https://www.ibytx.com/1792.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

相关推荐

  • 暂无文章

登录

找回密码

注册