Php (Wordpress)通过代码发布新帖子,添加缩略图和slug问题

Php (Wordpress)通过代码发布新帖子,添加缩略图和slug问题,php,wordpress,Php,Wordpress,我正在通过代码将一些静态页面导入我的WP网站,有两个问题我找不到任何解决方案 这是我的插入代码: $data = getPost(1); // details for the post we're about to insert $my_post = array( 'post_title' => $data['page_title'], 'post_date' => date('Y-m-d H:i:s',strtotime($data['date'])),

我正在通过代码将一些静态页面导入我的WP网站,有两个问题我找不到任何解决方案

这是我的插入代码:

$data = getPost(1);

// details for the post we're about to insert
$my_post = array(
    'post_title'  => $data['page_title'],
    'post_date'     => date('Y-m-d H:i:s',strtotime($data['date'])),
    'post_date_gmt' => date('Y-m-d H:i:s',strtotime($data['date'])),
    'post_content' => '<p>' . $data['intro'] . '</p>' . $data['body'],
    'post_status' => 'publish',
    //'post_category' => (!empty($cats[$data['category']]) ? $cats[$data['category']] :     1),
    'post_author' => (!empty($users[$data['author']]) ? $users[$data['author']] : 9),
    'post_name' => $data['slug'],
    'post_type' => 'post'
);
$data=getPost(1);
//我们将要插入的帖子的详细信息
$my_post=数组(
“贴子标题”=>$data[“页面标题”],
'post_date'=>date('Y-m-d H:i:s',strottime($data['date']),
'post_date_gmt'=>date('Y-m-d H:i:s',strottime($data['date']),
“post_content'=>”。$data['intro']。

。$data['body'], “发布状态”=>“发布”, //“post_category”=>(!empty($cats[$data['category']])?$cats[$data['category']]:1), “post_author'=>(!empty($users[$data['author']])?$users[$data['author']]:9), “post_name”=>$data['slug'], “职位类型”=>“职位” );
如果您想知道$data的内部是什么,请看以下内容:

数组([post_title]=>Gutscheine bei Bingo3X gewinnen[post_date]=>2013-12-08 00:00:00[post_date\u gmt]=>2013-12-08 00:00:00[post_内容]=> 没有Geld gewinnen macht Spaß,sondern Gutscheine können ebenfalls ihren Vorteil haben。因此,我们可以在30%gewinnen的Gurszeit Gutscheine中使用邮袋担保

没有Geld gewinnen macht Spaß,sondern Gutscheine können ebenfalls ihren Vorteil haben。因此,我们可以在30%gewinnen的Gurszeit Gutscheine中使用邮袋担保

[post_status]=>publish[post_author]=>9[post_name]=>gutscheine-bei-bingo3x-gewinnen[post_type]=>post)

  • 根据“post_name”将为帖子创建slug(链接到它),每当我导入此数据时,它都会正确添加帖子,但会在slug的末尾添加一些奇怪的数字,例如:mysite.com/2013/10/08/gutscheine-bei-bingo3x-gewinnen-5 要以编程方式插入后期特色图像

     $wp_filetype = wp_check_filetype(basename($filename), null );
    
        $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
        );
    
        $attach_id = wp_insert_attachment( $attachment, $filename, $parent_id );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    
    add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
    

    slug必须是唯一的,因此如果帖子名称重复,则必须修改slug才能使帖子有效。您尝试过这个吗?此外,还有一个类似的问题,在wp_插入_数据检查之前,尝试阅读更多关于它的信息,如果slug dosnt已经存在,则会延迟响应。周末。非常感谢你的帮助!荣誉
     $wp_filetype = wp_check_filetype(basename($filename), null );
    
        $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
        );
    
        $attach_id = wp_insert_attachment( $attachment, $filename, $parent_id );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    
    add_post_meta($post_id, '_thumbnail_id', $attach_id, true);