我正在以编程方式插入一个新页面,然后我想使用该新页面的ID添加模板,但是wp\\u insert\\u post没有返回任何ID,下面是我使用的代码
$tittle = \'My new page\';
$newPage = array(
\'post_type\' => \'page\',
\'post_title\' => $tittle,
\'post_status\' => \'publish\'
);
$post_id = wp_insert_post( $newPage);
print_r($post_id);
实际上,我是这样使用代码的
function criar_pag($titulo, $template = 0){
$a = get_page_by_title( $titulo, \'post\' );
if ($a == null || $a->post_status == \'trash\') {
$pagina = array(
\'post_type\' => \'page\',
\'post_title\' => $titulo,
\'post_status\' => \'publish\'
);
$post_id = wp_insert_post( $pagina);
update_post_meta($post_id, \'_wp_page_template\', $template);
}
}
criar_pag("my new page", "customtemplate.php");
最合适的回答,由SO网友:Sébastien Serre 整理而成
我刚刚在一个wp_body_open
它的工作原理是:
add_action( \'wp_body_open\', \'function_test\' );
function function_test() {
$tittle = \'My new page\';
$newPage = array(
\'post_type\' => \'page\',
\'post_title\' => $tittle,
\'post_status\' => \'publish\'
);
$post_id = wp_insert_post( $newPage);
print_r($post_id);
}
您在哪里使用此代码?