在哪里插入我们的WordPress帖子的其他站点图像URL

时间:2013-07-31 作者:user2560169

我正在将其他网站的内容废弃到wordpress网站,我做了所有的事情,下面是我的插入查询

$my_post = array(
   \'post_title\'    => $my_title,
   \'post_status\'   => \'publish\',
   \'post_author\'   => 1,
   \'post_category\' => array(5),
   \'post_content\'  => $res_final,
   \'post_date\'     => date(\'Y-m-d H:i:s\'),
   \'post_date_gmt\' => date(\'Y-m-d H:i:s\'),
   \'post_type\'     => \'post\'
 );

 $post_id = wp_insert_post( $my_post );
我可以插入我的标题和内容,是的,它正在显示:),甚至我可以看到图像插入到文章中,因为图像与内容一起出现,但我计划将图像单独从内容中取出,并单独存储为帖子图像,以便它将出现在头版。

关于如何将图像添加到帖子中的任何帮助,以及插入我的图像URL的最佳方式,一个插入查询来完成此操作。请帮帮我

1 个回复
SO网友:Pontus Abrahamsson

阅读关于wp_insert_attachment(), wp_update_attachment_metadata()wp_generate_attachment_metadata()

要添加附件,请使用wp_insert_attachment() 示例:

<?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 );
?>

结束

相关推荐

Group posts by meta_key

我对meta\\u值分组有问题。查询将查找元键为“company”的帖子。我想要一个独特的颜色列表,如:蓝、红、黄array\\u unique未成功,并且自定义mysql查询也未成功。<?php $args = array( \'category_name\' => $cat_name, \'posts_per_page\' => \'60\', \'paged\' => $current_page,&#x