全球主机交流论坛

标题: wp博客怎么把首图转换成封面图 [打印本页]

作者: glisten    时间: 2020-12-29 17:13
标题: wp博客怎么把首图转换成封面图
之前的主题是首图自动显示在封面上,换了个主题不支持首图显示封面,有什么插件可以批量设置首图是封面的吗
作者: 嗷嗷    时间: 2020-12-29 17:14
这个俺也不晓得


​​​​​​​
作者: 流河旱树    时间: 2020-12-29 17:19
加到functions.php文件里
  1. // Auto add featured image
  2. function wpsites_auto_set_featured_image() {
  3.    global $post;
  4.    $featured_image_exists = has_post_thumbnail($post->ID);
  5.       if (!$featured_image_exists)  {
  6.          $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  7.          if ($attached_image) {
  8.             foreach ($attached_image as $attachment_id => $attachment) {set_post_thumbnail($post->ID, $attachment_id);}
  9.          }
  10.       }
  11. }
  12. add_action('the_post', 'wpsites_auto_set_featured_image');
复制代码

作者: tl3821    时间: 2020-12-29 21:24
我是抓第一个img的,用那个wpjam啥的
作者: glisten    时间: 2020-12-29 22:25
tl3821 发表于 2020-12-29 21:24
我是抓第一个img的,用那个wpjam啥的

是安装这个插件吗,我的因为是用的图床,各种方案试了都不行
作者: tl3821    时间: 2020-12-29 22:53
glisten 发表于 2020-12-29 22:25
是安装这个插件吗,我的因为是用的图床,各种方案试了都不行

对 理论上 只要你那个 标签是 img 的就行了
不然就用楼上那个代码的

作者: glisten    时间: 2020-12-29 23:22
tl3821 发表于 2020-12-29 22:53
对 理论上 只要你那个 标签是 img 的就行了
不然就用楼上那个代码的

抓第一个img是设置自定义字段吗,具体怎么设置的




欢迎光临 全球主机交流论坛 (https://hostloc.gdisk.cf/) Powered by Discuz! X3.4