5

我正在为用户添加/编辑他们的帖子做前端管理员。我已经制作了帖子添加表单并且它可以工作,但是编辑表单不起作用。

函数.php

function add_new_post( $post_id )
{
    if( $post_id == 'new' ) {
        // Create a new post
        $post = array(
            'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
            'post_category' => array(4),
            'post_status'  => 'draft',
            'post_type'  => 'post'
        );

        // insert the post
        $post_id = wp_insert_post( $post );

        return $post_id;
    }
    else {
        return $post_id;
    }
}add_filter('acf/pre_save_post' , 'add_new_post' );

索引.php

<div id="updateform-<?php the_ID(); ?>" class="collapse">
    <?php
    echo get_the_ID();
    $args = array(
        'post_id' => get_the_ID(), // post id to get field groups from and save data to
        'field_groups' => array(31), // 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' => get_permalink( get_the_ID() ),
            '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( $args );
    ?>
</div>
4

3 回答 3

1

要保存帖子,您应该使用save_postnot pre_save_post。基本上pre_save_post用于新帖子。

在下面使用这个add_filter('save_post' , 'add_new_post');

于 2014-03-14T04:55:53.527 回答
0

我为此创建了插件:

  1. 表单操作 https://wordpress.org/plugins/forms-actions/

将操作添加到您的 ACF 表单。

  1. ACF 前端显示 https://wordpress.org/plugins/acf-frontend-display/

在前端显示您的 ACF 表单

于 2015-03-30T10:49:48.353 回答
0

如果您碰巧使用Elementor Page Builder,您可以通过安装Advanced Widgets for Elementor来做到这一点。它带有一个ACF Form小部件,您可以将其拖放到您网站的任何位置,并通过 UI 配置您的表单(无需编码)。

于 2019-03-23T04:34:36.663 回答