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

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

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

WordPress后台文章如何调用缩略图? (https://www.wp-admin.cn/) WordPress使用教程 第1张

  在自己的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网站时,也可以很轻松的调用文章缩略图片了。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-07-18 13:39:34
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: