首先我下载了 https://carbonfields.net/zip/latest/并在 WP 后端安装了该插件。我也激活了。
\n\n对于这个测试用例,我使用“二十十六”模板进行全新安装的 WordPress,没有安装任何其他插件,并且根据Carbon Fields 的文档页面,我将以下代码添加到了 function.php 文件的顶部:
\n\n<?php // PHP 7\nuse Carbon_Fields\\Container;\nuse Carbon_Fields\\Field;\n\nadd_action( \'carbon_fields_register_fields\', \'crb_attach_theme_options\' );\nfunction crb_attach_theme_options() {\n Container::make( \'theme_options\', \'Theme Options\' )\n -> set_page_menu_position( 0 )\n -> add_fields( array(\n Field::make( \'text\', \'crb_text\')\n ) );\n}\nRun Code Online (Sandbox Code Playgroud)\n\n到目前为止,一切看起来都很好,因为“主题选项”正如预期一样出现在 WP 后端中。
\n\n\n\ncrb_text现在我尝试按如下方式检索字段值:
// this snippet starts exactly where the previous one ended\nadd_action( \'after_setup_theme\', \'crb_load\' );\nfunction crb_load() {\n // require_once( ABSPATH . \'/vendor/autoload.php\' ); original from website throws: "Failed opening …Run Code Online (Sandbox Code Playgroud) 我为主题选项创建容器并向其中添加字段数组。然后打开此选项页面,查看标题和应添加字段的空块。如何显示字段?
碳田2.1.0
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'theme_options', __( 'Theme Options', 'crb' ) )
->add_fields( array(
Field::make( 'text', 'crb_text', 'Text Field' ),
) );
Run Code Online (Sandbox Code Playgroud)