我正在创建一个字段,需要为网站上的每个帖子添加一个自定义字段。我正在插件中使用ACF插件来实现这一点。我跟踪了this tutorial 我很接近。在教程中,他们将自定义字段添加到设置中,但我想将自定义字段添加到每篇文章中。以下是我拥有的与ACF相关的代码:
<?php
// 1. customize ACF path
add_filter(\'acf/settings/path\', \'my_acf_settings_path\');
function my_acf_settings_path( $path ) {
// update path
$path = get_stylesheet_directory() . \'/vendor/advanced-custom-fields/\';
// return
return $path;
}
// 2. customize ACF dir
add_filter(\'acf/settings/dir\', \'my_acf_settings_dir\');
function my_acf_settings_dir( $dir ) {
// update path
$dir = get_stylesheet_directory_uri() . \'/vendor/advanced-custom-fields/\';
// return
return $dir;
}
// 3. Hide ACF field group menu item
add_filter(\'acf/settings/show_admin\', \'__return_false\');
// 4. Include ACF
include_once( get_stylesheet_directory() . \'/vendor/advanced-custom-fields/acf.php\' );
// 5. Setup Custom Fields on post pages
$this->setup_options();
public function setup_options() {
if(function_exists("register_field_group")) {
register_field_group(array (
\'id\' => \'acf_field-id\',
\'title\' => \'Field Name\',
\'fields\' => array (
array (
\'key\' => \'field_5ab8f6946d890\',
\'label\' => \'Label\',
\'name\' => \'name\',
\'type\' => \'wysiwyg\',
\'instructions\' => \'\',
\'default_value\' => \'\',
\'toolbar\' => \'full\',
\'media_upload\' => \'yes\',
),
),
\'location\' => array (
array (
array (
\'param\' => \'post_type\',
\'operator\' => \'==\',
\'value\' => \'post\',
\'order_no\' => 0,
\'group_no\' => 0,
),
),
),
\'options\' => array (
\'position\' => \'normal\',
\'layout\' => \'no_box\',
\'hide_on_screen\' => array (
),
),
\'menu_order\' => 0,
));
}
}
我知道我的问题在上面的第5页。我不清楚如何运行函数为每个帖子加载该字段。有什么见解吗?
此外,我将如何在单个字段中自动输出此字段代码。帖子内容之前的php模板。我可以使用WordPress函数也这样做吗?