(文字出版社)通过代码发布新帖子,添加缩略图和辅助信息网络问题


(Wordpress) New post via code, adding thumbnail and slug issues

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

这是我的插入代码:

$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里面有什么,这里是:

数组 ( [post_title] => Gutscheine bei Bingo3X gewinnen [post_date] => 2013-12-08 00:00:00 [post_date_gmt] =>

2013-12-08 00:00:00 [post_content] => Nicht nur Geld gewinnen macht Spaß, sondern Gutscheine können ebenfalls ihren Vorteil haben.So gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30 £ gewinnen.

Nicht nur Geld gewinnen macht Spaß, sondern Gutscheine können ebenfalls ihren Vorteil haben.So gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30 £ gewinnen.

[post_status] => 发布 [post_author] => 9 [post_name] =>

Gutscheine-bei-bingo3x-gewinnen [post_type] => post (

  1. 根据 http://codex.wordpress.org/Function_Reference/wp_insert_post"post_name"将为帖子创建 slug(链接到它(,每当我导入此数据时,它都会正确添加帖子,但在 slug 的末尾附加一些奇怪的数字,例如它可以是:mysite.com/2013/10/08/gutscheine-bei-bingo3x-gewinnen-5 <--- -5 不知从何而来......有什么想法吗?:)

  2. 更大的问题。如何通过代码添加featured_image?我有URL(或者我可以将图像定位在本地,我有尺寸,但我找不到如何在帖子中插入它。某种add_post_meta或wp_set_object_terms可以提供帮助?但我不确定在那里写什么...

谢谢!!

    if( null == get_page_by_title( $title_connection ) ) { 
// $title_connection should be unique
            $post_id = wp_insert_post(
                array(
                    'comment_status'    =>  'closed',
                    'ping_status'       =>  'closed',
                    'post_author'       =>  $author_id,
                    'post_name'         =>  $slug_connection,
                    'post_title'        =>  $title_connection,
                    'post_status'       =>  'publish',
                    'post_type'         =>  'page',
                    'post_parent'       =>  $page_parent->ID
                )
            );

        }

要以编程方式插入帖子特色图像...

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