我正在通过代码将一些静态页面导入我的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里面有什么,那么它就是:
数组([post_title] =&gt; Gutscheine bei Bingo3X gewinnen [post_date] =&gt; 2013-12-08 00:00:00 [post_date_gmt] =&gt; 2013-12-08 00:00:00 [post_content] =&GT; Nicht nur Geld gewinnenmachtSpaß,sondernGutscheinekönnenebenfallsihren Vorteil haben。所以gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30£gewinnen。
Nicht nur Geld gewinnenmachtSpaß,sondernGutscheinekönnenebenfallsihren Vorteil haben。所以gibt es mit der Pouch-A-Vouch Aktion bei Bingo3X zurzeit Gutscheine im Wert von 30£gewinnen。
[post_status] =&gt;发表[post_author] =&gt; 9 [post_name] =&gt; gutscheine-bei-bingo3x-gewinnen [post_type] =&gt; ())
根据http://codex.wordpress.org/Function_Reference/wp_insert_post“post_name”将为帖子创建slug(链接到它),每当我导入此数据时,它都会正确地添加帖子,但会在末尾添加一些奇怪的数字。 slu ,,它可以是:mysite.com/2013/10/08/gutscheine-bei-bingo3x-gewinnen-5&lt; ---那个-5来自哪儿..有什么想法吗? :)
更大的问题。如何通过代码添加featured_image?我有URL(或者我可以将图像放在本地,我有尺寸,但我找不到如何用帖子插入它。 某种add_post_meta或wp_set_object_terms可以提供帮助吗?但我不确定在那里写什么......
谢谢!
答案 0 :(得分:1)
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);