如何在前端表单发布中显示wordpress acf自定义字段

use*_*840 5 php wordpress advanced-custom-fields

在我的项目中,我需要在前端显示自定义帖子字段表单,因此我安装了 ACF 并创建了自定义字段,现在我的问题是如何将这些字段与 HTML 一起呈现???我使用了 get_fields() 函数,但它仍然不会显示任何 HTML 代码。

小智 5

如果您想显示帖子内的字段,则必须将代码放置在“single.php”循环内,假设您使用的是标准帖子类型。

此代码仅检索字段,它不会显示任何内容,它用于将值存储在变量中:

get_field('field-name');
Run Code Online (Sandbox Code Playgroud)

要使字段显示在模板中,您必须使用以下命令:

the_field('field-name');
Run Code Online (Sandbox Code Playgroud)

您还可以将代码插入用于显示帖子的存档模板或查询帖子中。

这也可以工作:

echo get_field('field-name')
Run Code Online (Sandbox Code Playgroud)

或者

$myfield = get_field('field-name');
echo $myfield;
Run Code Online (Sandbox Code Playgroud)


Aks*_*dar 0

看看这个文档...

您可以使用以下功能添加字段或完整表单,

$options = array(
    'post_id' => $post->ID, // post id to get field groups from and save data to
    'field_groups' => array(), // 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 );
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助...