使用ACF内置方法更新自定义字段

2018-07-31

此函数用于更新特定帖子的字段值。

<?php update_field($selector, $value, $post_id); ?>
  • $selector (string) the field name or key (required)
  • $value (mixed) the value to save (required)
  • $post_id (mixed) Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

基本用法

此示例演示如何更新当前视图中被称为“视图”的字段的值。

<?php

// get current count value
$count = (int) get_field('views');


// increase
$count++;


// update
update_field('views', $count);
    
?>

Update a value from different objects

This example shows a variety of $post_id values to update a value from a post, user, term and option.

$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above

$value = update_field( 'my_field', 'my_value', $post_id );

Advanced

This example will demonstrate how to create a new post, and save multiple field values to it.

<?php 

// vars
$my_post = array(
	'post_title'	=> 'My post',
	'post_type'		=> 'post',
	'post_status'	=> 'publish'
);


// insert the post into the database
$post_id = wp_insert_post( $my_post );


// save a basic text value
$field_key = "field_123456";
$value = "some new string";
update_field( $field_key, $value, $post_id );


// save a checkbox or select value
$field_key = "field_1234567";
$value = array("red", "blue", "yellow");
update_field( $field_key, $value, $post_id );


// save a repeater field value
$field_key = "field_12345678";
$value = array(
	array(
		"sub_field_1"	=> "Foo",
		"sub_field_2"	=> "Bar"
	)
);
update_field( $field_key, $value, $post_id );


// save a flexible content field value
$field_key = "field_123456789";
$value = array(
	array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ),
	array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" )
);
update_field( $field_key, $value, $post_id );

?>

 

WordPress开发的相关文章

polylang 注册字符串翻译
关于插件的安装就不多说了,重点说下小型站点在多语言开发过程中遇到的一个关键性问题,就是对于主题页面中的装饰性文字,向导性文字
WordPress 后台用户根据注册时间排序
在当前主题的 functions.php 中添加以下 php 代码即可: //WordPress 后台用户根据注册时间排序
wordpress多语言插件polylang常用代码
获得当前语言的代码: <?php pll_current_language(); ?> 如何调用指定语言的文
免插件禁用古滕堡编辑器(Gutenberg)
在functions.php底部新增以下代码 //禁止 WordPress5.0 使用 Gutenberg 块编辑器 a
wordpress分页函数:the_posts_pagination
wordpress免插件分页,wordpress自带函数:the_posts_pagination <?php th
利用wp_insert_post函数插入数据
在主题开发的过程中可能需要在前台提交数据到后台,比如前台投稿,项目提交等,这个使用利用wp_insert_post函数可以轻
调用wordpress编辑器
<?php wp_editor( $content, $editor_id, $settings = array()
利用update_user_meta()函数增加修改wordpress用户个人信息
在开发过程中我们需要对用户信息额外的字段进行更新和修改,或者增加,利用一些wordpress内置的函数就可以轻松完成: u
wordpress用自定义分类法获取自定义文章类型分类列表
<?php $args=array( 'taxonomy' => 'development_cat',
dynamic主题contactform7表单设置参考
开发项目表单设置参考: <div class="row"> <div class="col-xs-12