如何在Wordpress中使用ACF创建帖子?
为了保存帖子,我在function.php中添加了以下内容:如何在Wordpress中使用ACF创建帖子?,wordpress,plugins,frontend,advanced-custom-fields,posting,Wordpress,Plugins,Frontend,Advanced Custom Fields,Posting,为了保存帖子,我在function.php中添加了以下内容: function my_pre_save_post( $post_id ) { // check if this is to be a new post if( $post_id != 'new' ) { return $post_id; } // Create a new post $post = array( 'post_status' =>
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => 'A title, maybe a $_POST variable' ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
在page.php上,我得到了以下代码:
$postid = get_the_ID();
if($postid ==50){ //50 is a Page I created for the Form
$options = array(
'post_id' => 'new',//$post->ID, // post id to get field groups from and save data to
'field_groups' => array(46), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $options );
}
Wordpress的ACF插件有很好的文档记录,但我无法解决我的问题。
文档中说,如果要创建新帖子,必须将
$options
数组中的post\u id
值设置为new\u post
,而不是new
。此外,还必须将new\u post
array键与包含新post数据的数组一起用作键的值。
查看文档。这就是我的基本意思:
$options = array(
'post_id' = 'new_post',
'new_post' = array(
//new post data
)
);
$options = array(
'post_id' = 'new_post',
'new_post' = array(
//new post data
)
);