我编写了一个函数,可以在页面中插入内容,并且每个日期都会自动更新。
我用过wp_update_post( $my_post );
更新页面。
我想先在顶部特定的页面中插入内容,然后更新,但它不起作用。
它只是更新或插入内容。。
function wp_emallmobnok(){
$postdater = parsidate(\'j / F / Y\',$datetime=\'now\',$lang=\'pre\');
$postdate = date(\'Y-m-d H:i:s\');
$ta = parsidate(\'j / F / Y\',$datetime=\'now\',$lang=\'pre\');
$postdate_gmt = date(\'Y-m-d H:i:s\');
$titles="price daily";
$posts = array(
\'post_content\' => $oiobz1,
\'post_name\' => $titles,/// The page url name
\'ID\' => 225, /// The page id witch we want to update that
\'post_title\' => $titles,
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'post_author\' => \'6\',
\'ping_status\' => \'open\',
\'post_date\' => $postdate_gmt,
\'post_category\' => array(188),
\'tags_input\' => " price",
);
$post_id = wp_insert_post($posts);//// instert post
add_post_meta( $post_id, \' wp_insert_post\', 0, true );
$post_up = wp_update_post($posts);///update post
add_post_meta( $post_id, \' wp_update_post\', 0, true );
}
最合适的回答,由SO网友:Rene Korss 整理而成
正如Codex所说wp_update_post
:
要按预期工作,必须传递要更新的帖子的ID。
填写ID字段并不是绝对必要的,但如果没有它,使用该函数就没有什么意义。
因此,您必须将新创建的帖子ID添加到$posts
更新它。
function wp_emallmobnok(){
$postdater = parsidate(\'j / F / Y\',$datetime=\'now\',$lang=\'pre\');
$postdate = date(\'Y-m-d H:i:s\');
$ta = parsidate(\'j / F / Y\',$datetime=\'now\',$lang=\'pre\');
$postdate_gmt = date(\'Y-m-d H:i:s\');
$titles ="price daily";
// Post data
$posts = array(
\'post_content\' => $oiobz1,
\'post_name\' => $titles,
\'post_title\' => $titles,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'post_author\' => \'6\',
\'ping_status\' => \'open\',
\'post_date\' => $postdate_gmt,
\'post_category\' => array(188),
\'tags_input\' => " price",
);
$post_id = wp_insert_post($posts);//// instert post
add_post_meta( $post_id, \' wp_insert_post\', 0, true );
// Update post if inserting was successful
if( $post_id !== 0 && !is_wp_error( $post_id ) ){
// Add post ID to post data
$posts[\'ID\'] = intval( $post_id ); // ID has to be integer
$post_up = wp_update_post($posts);///update post
add_post_meta( $post_id, \' wp_update_post\', 0, true );
}
else if( is_wp_error( $post_id ) ){
$error_string = $post_id->get_error_message();
echo \'ERROR: \'.$error_string;
}
}