mcguffin / acf-wp-objects
整合WordPress和ACF
Requires
- php: >=8.1.0
- composer/installers: ~2.0
- dev-master
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.39
- 0.6.38
- 0.6.37
- 0.6.36
- 0.6.35
- 0.6.34
- 0.6.33
- 0.6.32
- 0.6.31
- 0.6.30
- 0.6.29
- 0.6.28
- 0.6.27
- 0.6.26
- 0.6.25
- 0.6.24
- 0.6.23
- 0.6.22
- 0.6.21
- 0.6.20
- 0.6.19
- 0.6.18
- 0.6.17
- 0.6.16
- 0.6.15
- 0.6.14
- 0.6.13
- 0.6.12
- 0.6.11
- 0.6.10
- 0.6.9
- 0.6.8
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.22
- 0.5.21
- 0.5.20
- 0.5.19
- 0.5.18
- 0.5.17
- 0.5.16
- 0.5.15
- 0.5.14
- 0.5.13
- 0.5.12
- 0.5.11
- 0.5.10
- 0.5.9
- 0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.25
- 0.4.24
- 0.4.23
- 0.4.22
- 0.4.21
- 0.4.20
- 0.4.19
- 0.4.18
- 0.4.17
- 0.4.16
- 0.4.15
- 0.4.14
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- dev-dependabot/npm_and_yarn/json5-2.2.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/minimatch-3.1.2
This package is auto-updated.
Last update: 2024-09-15 14:52:52 UTC
README
填补WordPress和ACF之间的空白。
功能
将WordPress对象整合到ACF中。
- 使用ACF字段编辑WP-Post属性
- 设置:blogname
- 设置:blogdescription
- 帖子:post_title
- 帖子:post_excerpt
- 帖子:post_content
- 帖子:post_thumbnail
- 帖子:attachments
- 术语:term_name
- 术语:term_description
- 主题-模:custom_logo
- 新的 字段类型
- 帖子类型
- 分类法
- 图片大小
- Includer(包含来自不同字段组的所有字段)
- 用户角色
- Sweet Spot(与媒体一起使用)
- 插件模板选择(从插件中选择模板文件)
- 新的 位置规则
- 帖子类型/分类法是公开的/内置的/显示UI/显示在菜单中/显示在导航菜单中
- 帖子类型支持
- 编辑器是经典/块编辑器
- WP选项页面是通用/写作/阅读/讨论/媒体/永久链接
- 插件模板设置
- 到处
- 选择字段:从重复器中获取选择,例如从ACF选项页面
- 多站点:
- 使用
acf_add_options_(sub_)page()
添加网络管理员页面。将"network" => true,
添加到页面参数中(ACF Pro仅限 – 两个插件都必须启用网络功能)
- 使用
- 样式:
- 在块编辑器侧边栏中提供更紧凑的样式
- 将类
no-head
和no-sort
添加到重复器中 - 将类
seamless
和no-label
添加到字段中 - 将类
button-group
添加到复选框中(使它们看起来像按钮组)
- 页面布局:提供位置规则的通用灵活内容字段。使用
acf_add_page_layout('foobar')
初始化,使用acf_page_layouts('foobar')
渲染 - JSON-Paths:在自定义位置(如插件)中保存字段组JSON
- 本地化:通过po文件本地化ACF字段标签
- ACF选项页面:可选地,在后台和从WPCLI中进行重置、导出和导入。
- 标签字段通过URL打开标签。只需追加
#Tab Label
安装
生产(使用Github Updater – 建议用于多站点)
- 转到 版本发布
- 下载 'acf-wp-objects.zip'
- 上传并激活它,就像其他任何WordPress插件一样
- 只要插件处于活动状态,就会运行自动更新
开发
- 进入您的插件目录
- $
git clone git@github.com:mcguffin/acf-wp-objects.git
- $
cd acf-wp-objects
- $
npm install
- $
npm run dev
JSON-Paths
考虑以下场景:您正在使用主题中的本地json字段组。您想在子主题中覆盖它们。或者,您有一个具有ACF依赖的插件,包含作为本地json的字段组。
这将从主题和子主题目录中的子目录 path/to/json-files
加载并保存ACF JSON,但仅当字段组键为 group_my_fieldgroup_key
时。
acf_register_local_json( 'path/to/json-files', // e.g. 'acf-json' in a theme function( $field_group ) { // callback // return true, if the field group JSON // should be saved at the given location return $field_group['key'] === 'group_my_fieldgroup_key'; }, [ // parent paths to check get_template_directory(), get_stylesheet_directory(), ] );
Sweet Spot 字段
为具有 object-fit: cover
的图像选择合适的裁剪方式的一种简单方法。
要为所有图像启用“甜点”功能,以下是一段 PHP 代码,位于您的主题的 functions.php
或 mu-plugin 中。
add_filter( 'acf_image_sweetspot_enable', '__return_true' );
插件将注册一个包含甜点字段和在前端为图像属性添加内联样式规则的图像附件字段组,例如 object-position: 12.3% 81.4%;
。
JSON I18n
ACF 提供对 WPML 的支持以本地化字段组。ACF WP Objects 通过 .po
文件提供不同的方法。
acf_localize_field_groups( 'my-textdomain', function( $field_group ) { // callback which should return true, if the field group // localization is available under the given textdomain return $field_group['key'] === 'group_my_fieldgroup_key'; });
如果您使用本地 JSON,以下是一个节点脚本,允许您提取字符串 src/run/json-i18n.js
并将它们添加到 pot 文件中
安装 WP CLI。
将 src/run/json-i18n.js
和 src/run/lib/json-extract.js
放在您的包目录中。
从 JSON 文件中提取字符串并将它们添加到 PHP 文件中
node ./src/run/json-i18n.js 'my-texdomain' ./path/to/json ./php-output.php
使用 WP CLI 生成 pot
wp i18n make-pot . languages/my-textdomain.pot --domain=my-textdomain
模板文件(仅限 ACF Pro)
- 过滤模板类型
add_filter('acf_wp_objects_template_types', function( $types ) { $slug = 'foo-plugin'; $key = 'Items Template'; $theme_location = 'foo-plugin'; // will point to wp-content/themes/<current-theme>/foo-plugin/ // default: $slug $plugin_location = 'templates'; // will point to wp-content/plugins/foo-plugin/templates/ // null: use default, false: no plugin location, string: custom location inside plugin $types[ $slug ] = [ 'header_key' => $key, 'theme_location' => $theme_location, 'plugin_location' => $plugin_location, ]; return $types; });
WP Objects 将在主题和插件位置中搜索具有标题键的模板文件。
- 创建一个名为
my_fabulous_template
的模板选择字段。使用方式如下:get_template_part( get_field('my_fabulous_template') );
- 将一些模板文件放在位置
/* Items Template: List */ $settings = get_field('my_fabulous_template_settings');
页面布局(仅限 ACF Pro)
生成一个灵活的内容字段并将字段组转换为布局。如果您需要可扩展的布局集来选择,则此功能非常理想。
- 添加一个布局部分
acf_add_page_layout([ 'title' => 'My Layout', 'name' => 'my-layout', ]);
- 创建字段组。将“页面布局”设置为“我的布局”作为位置,并在底部输入行布局别名。
- 在您的主题中创建与上述别名对应的模板文件。文件名应匹配
acf/layout-<row_layout_slug>.php
。别忘了使用get_sub_field()
,您正在灵活内容字段内部! - 在您的页面.php 模板文件中,在循环内部调用此代码
acf_page_layouts( 'my-layouts' );
重复选择与重复行返回
ACF 默认不加载前端字段组。要在前端检索重复选择字段的值,ACF 必须知道这一点。
您可以通过添加 do_action( 'acf_wpo_load_fields' );
来实现这一点
ACF 选项页面
为 acf_add_options_page()
和 acf_add_options_sub_page()
提供了一些新选项。
acf_add_options_page([ 'import' => false, 'import_message' => __( 'Options Imported', 'acf-wp-objects' ), 'import_error_message' => __( 'Invalid Import Data', 'acf-wp-objects' ), 'import_button' => __( 'Import', 'acf-wp-objects' ), 'import_select_file' => __( 'Select File…', 'acf-wp-objects' ), 'export' => false, 'export_references' => false, 'export_button' => __( 'Export Settings', 'acf-wp-objects' ), 'reset' => false, 'reset_button' => __( 'Restore defaults', 'acf-wp-objects' ), 'reset_message' => __( 'Options Reset to Defaults', 'acf-wp-objects' ), ]);
参数
import
Boolean
启用导入功能import_message
String
成功导入后的消息import_error_message
String
导入失败后的消息import_button
String
导入按钮标签import_select_file
String
导入文件输入标签export
Boolean|Array
启用导出功能。true
将简单地从当前选项页导出值。传递一个选项页 ID 数组将一次性从多个选项页导出export_references
Boolean
是否导出引用的内容,如帖子、图像或术语export_button
String
导出按钮标签reset
Boolen|String
启用重置功能。传递有效导入文件的路径将导入它。reset_message
String
成功重置后的消息reset_button
String
重置按钮标签
示例
启用导入和导出。
acf_add_options_page([ 'page_title' => 'Configure Foobar Options', 'menu_title' => 'Foobar Options', 'post_id' => 'foobar_options', 'parent_slug' => 'themes.php', 'menu_slug' => 'foobar-options', 'import' => true, 'export' => true, ]);
同时启用重置。
acf_add_options_page([ 'page_title' => 'Configure Foobar Options', 'menu_title' => 'Foobar Options', 'post_id' => 'foobar_options', 'parent_slug' => 'themes.php', 'menu_slug' => 'foobar-options', 'import' => true, 'export' => true, 'reset' => true, ]);
启用重置并从导出文件获取值。
acf_add_options_page([ 'page_title' => 'Configure Foobar Options', 'menu_title' => 'Foobar Options', 'post_id' => 'foobar_options', 'parent_slug' => 'themes.php', 'menu_slug' => 'foobar-options', 'import' => true, 'export' => true, // pass file path to reset 'reset' => get_template_directory().'/foobar-defaults.json', ]);
WP-CLI
创建一个导出文件
wp acf-options-page export foobar-options --pretty > wp-content/themes/my-theme/foobar-defaults.json
从文件导入选项
wp acf-options-page import wp-content/themes/my-theme/foobar-defaults.json
重置选项页面。
无论传递给 acf_add_options_page()
的配置如何,此操作都不会从文件中导入值。使用 wp acf-options-page import
来执行此操作。
wp acf-options-page reset foobar-options