如何在WordPress上使用php设置图像作为帖子的特色图像?

我想将图像设置为帖子的特色图像.我在wordpress文档中找到了这段代码,它将图像保存在上传目录中,但图像不再设置为帖子的特色图像(代码中的37).你能看一下吗?非常感谢?php$wp_filetype = wp_check_filetype(basena...

我想将图像设置为帖子的特色图像.我在wordpress文档中找到了这段代码,它将图像保存在上传目录中,但图像不再设置为帖子的特色图像(代码中的37).

你能看一下吗?非常感谢

<?php
  $wp_filetype = wp_check_filetype(basename($filename), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
     'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
     'post_content' => '',
     'post_status' => 'inherit'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
  // you must first include the image.php file
  // for the function wp_generate_attachment_metadata() to work
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id, $attach_data );
?>

解决方法:

据我所知你想在图片右侧使用图片作为特色图片?
那你为什么不在帖子上使用后端精选图片浏览选项.在那里你可以简单地将图像作为特色上传,你可以在你的页面上用这个代码显示特色图像the_post_thumbnail($size,$attr);

本文标题为:如何在WordPress上使用php设置图像作为帖子的特色图像?

基础教程推荐