为什么要使用自定义字段? (Why Use Custom Fields?)

I can’t begin to express how blown my mind was when I got started with Advanced Custom Fields (ACF). When I started using WordPress, I figured out how to bend Posts and Pages to do a whole lot more than they should have. I’d give clients very specific instructions for using Categories and Parent Pages in convoluted ways. Then I discovered custom post types and my instructions got a whole lot simpler.

当我开始使用Advanced Custom Fields(ACF)时,我无法表达自己的想法。 当我开始使用WordPress时,我想出了如何使Posts and Pages做得比其应有的要多得多的工作。 我会给客户非常具体的说明,以复杂的方式使用类别和父页面。 然后,我发现了自定义帖子类型 ,我的说明变得简单了很多。

But even with separate post types, not every piece of content fits neatly into a title+content+excerpt+thumbnail mold. Events require start dates and times. Employees often need email addresses or titles. Products might have prices, color variations or sizes. All these content types push WordPress beyond simple custom post types.

但是,即使采用不同的帖子类型,也并非每个内容都可以完美地适合标题+内容+摘录+缩略图模型。 活动需要开始日期和时间。 员工经常需要电子邮件地址或职务。 产品可能有价格,颜色变化或尺寸。 所有这些内容类型使WordPress超越了简单的自定义帖子类型。

插件呢? (What About Plugins?)

One solution to this is to find a plugin for each and every kind of custom content you need. Install WooCommerce for products, The Events Calendar for events, Meet My Team for employees, Soliloquy for slides, or WPP for portfolio items. That works for a lot of people: it’s faster to develop that way at first.

一种解决方案是为您需要的每种自定义内容找到一个插件。 为产品安装WooCommerce,为事件安装“事件日历”,为员工安装“遇见我的团队”,为幻灯片安装Soliloquy,为投资组合项目安装WPP。 这对很多人都有效:一开始开发这种方式会更快。

However, I’m not a fan of plugin smorgasbords for two reasons. First, every plugin you add affects site speed (hardly any speed it up!). Chances are high that every content type plugin you add will add its own JS and CSS files to the front end. You can literally watch your Page Speed score plummet as you add those plugins. Second, most plugins you use will look different from your theme (with the exception of premium themes that are “compatible” with major plugins). This will have one of two negative results: either your site will look like it’s been patched together from three or four plugins, or you’ll spend a ton of time overriding plugin CSS to make plugin content types look like they belongs in your theme.

但是,由于两个原因,我不喜欢smorgasbords插件。 首先,您添加的每个插件都会影响网站速度(几乎没有提高速度!)。 很可能您添加的每个内容类型插件都会在前端添加其自己的JS和CSS文件。 添加这些插件时,您可以从字面上看您的Page Speed得分直线下降。 其次,您使用的大多数插件看起来与您的主题有所不同(与主要插件“兼容”的高级主题除外)。 这将产生两个负面结果之一:您的网站看起来像是由三个或四个插件打补丁而成的,或者您将花费大量时间重写插件CSS,以使插件内容类型看起来像它们属于主题一样。

ACF字段类型 (ACF Field Types)

Advanced Custom Fields (ACF) provides a way to attach almost any kind of custom field imaginable to your post types.


包含的字段类型 (Included Field Types)

The ACF plugin comes with more than 20 field types by default:


Custom field types
  • Text: Single line text field, optional HTML support, character limit

    文本 :单行文本字段,可选HTML支持,字符限制

  • Text Area: Multi-line text field, optional HTML support, character limit

    文本区域 :多行文本字段,可选HTML支持,字符限制

  • Number: Number, min/max values

    数量 :数量,最小/最大值

  • Email: Validates email input

    电子邮件 :验证电子邮件输入

  • Password: Masks text for password input

    密码 :遮罩输入密码的文字

  • Wysiwyg Editor: Optional WP media insertion, basic/full editor buttons

    所见即所得编辑器 :可选的WP媒体插入,基本/完整编辑器按钮

  • Image: Upload image or select from WP library, returns object, url or ID

    图片 :上传图片或从WP库中选择图片 ,返回对象,URL或ID

  • File: Upload file or select from WP library, returns object, url or ID

    文件 :上传文件或从WP库中选择文件,返回对象,URL或ID

  • Select: Provide options in drop-down, supports multi-select

    选择 :在下拉菜单中提供选项,支持多选

  • Checkbox: Allow selection of multiple items in checkbox list

    复选框 :允许选择复选框列表中的多个项目

  • Radio Button: Allow selection of single item from radio button list

    单选按钮 :允许从单选按钮列表中选择单个项目

  • True / False: Simple true / false selector

    正确/错误 :简单的正确/错误选择器

  • Page Link: Returns URL of any post or page, can be limited to specific post types

    页面链接 :返回任何帖子或页面的URL,可以限制为特定的帖子类型

  • Post Object: Returns WP object of any post or page, can be limited to specific post types or taxonomies

    帖子对象 :返回任何帖子或页面的WP对象,可以限制为特定的帖子类型或分类法

  • Relationship: Same as post object, but with improved interface and drag-drop reordering

    关系 :与发布对象相同,但界面和拖放重新排序得到改进

  • Taxonomy: Returns object or ID for one or more taxonomy terms, can be limited to tags, categories or custom taxonomies

    分类法 :返回一个或多个分类法术语的对象或ID,可以限制为标签,类别或自定义分类法

  • User: Select one or more users, can be limited by role

    用户 :选择一个或多个用户,可以受角色限制

  • Google Map: Set lat/long center, zoom level, and width

    Google Map :设置纬度/经度中心,缩放级别和宽度

  • Date Picker: Select calendar date, return formatted date string

    日期选择器 :选择日历日期,返回格式化的日期字符串

  • Color Picker: Select hexadecimal color

    拾色器 :选择十六进制颜色

  • Message: No options, leave a message for users

    留言 :无选项,给用户留言

  • Tab: Collect following fields into a tab interface

    标签 :将以下字段收集到标签界面中

免费附加字段 (Free Add-on Fields)

The WordPress plugin repo and the ACF add-ons page have a number of helpful add-on plugins for ACF. These plugins allow users to select a menu, select a sidebar select a Gravity Form, collapse complex field groups and import spreadsheets.

WordPress插件库和ACF插件页面上有许多有用的ACF插件。 这些插件使用户可以选择菜单选择侧边栏, 选择“重力表”折叠复杂的字段组导入电子表格

付费附加字段 (Paid Add-on Fields)

There are a few premium ACF add-ons that are very useful. Most cost around $25 and are well worth it, in my opinion.

有一些非常有用的高级ACF附加组件。 我认为大多数产品的价格都在25美元左右,非常值得。

Repeater: The ACF Repeater Field allows users to develop complex layouts with any number of fields. For instance, a repeater field could contain a Text field, an Image field and a WYSIWYG field, allowing users to create a list of “Services” with icon, title and description.

中继器: ACF 中继器字段允许用户使用任意数量的字段来开发复杂的布局。 例如,转发器字段可以包含文本字段,图像字段和所见即所得字段,从而允许用户创建带有图标,标题和说明的“服务”列表。

Flexible Content: The ACF Flexible Content Field allows users to set up multiple layouts (each made of multiple fields) which can then be selected and used in any order on the page. See this Snow Fall tutorial for an example of Flexible Content fields in action.

弹性内容: ACF 弹性内容字段允许用户设置多个布局(每个布局由多个字段组成),然后可以在页面上以任何顺序选择和使用这些布局。 有关实际的“灵活内容”字段的示例,请参见此Snow Fall教程

Options Page: The ACF Options Page add-on allows developers to create custom options pages in the Dashboard, then attach any set of custom fields to those pages. This is quite useful for collecting site-wide data, such as company address, social media links, phone number or theme color options.

选项页面: ACF 选项页面插件允许开发人员在仪表板中创建自定义选项页面,然后将任何一组自定义字段附加到这些页面。 这对于收集站点范围的数据非常有用,例如公司地址,社交媒体链接,电话号码或主题颜色选项。

将字段组附加到内容 (Attaching Field Groups to Content)

ACF Field Groups can be attached to content by any criteria: post type, page template, parent page, post category or tag, taxonomies themselves, media, and users.


Setting field group locations

On the Edit page for the content that the field group is attached to, any pieces of the default WordPress admin can be hidden (Editor, Featured Image, Slug, Author, etc), and the Field Group can be place below the Title, below the Editor, or in the right column.


在模板中使用ACF字段 (Using ACF Fields in Templates)

ACF provides several useful PHP functions for including custom field data in WordPress template files. The ACF Documentation pages have a great deal of useful information. The following are some of the most common functions used.

ACF提供了一些有用PHP函数,用于将自定义字段数据包括在WordPress模板文件中。 ACF文档页面包含大量有用的信息。 以下是一些最常用的功能。

get_field() (get_field())

The get_field('field_name', $post->ID, 'option') function returns the formatted contents of the custom field whose name is passed as a parameter. A post ID parameter is optional: if this function is being used outside the loop or to access fields from another post or page, the ID would be necessary. If the field is on an Options Page, include the 'option' inside this function.

get_field('field_name', $post->ID, 'option')函数返回名称作为参数传递的自定义字段的格式化内容。 帖子ID参数是可选的:如果在循环外使用此函数或访问另一个帖子或页面中的字段,则ID是必需的。 如果该字段位于选项页面上,则在此函数内包括'option'

Since this function returns data, it can be assigned to a variable, or displayed with print or echo.


if ( get_field('sub_title') ) :
	print '<h2 class="sub-title">'.get_field('sub_title').'</h2>;

the_field() (the_field())

The the_field() function echos the contents of the field, formatted according to the parameters in the Custom Field Group admin area. It can take the same $post->ID and 'option' parameters as get_field(). It’s basically the same thing as echo get_field().

the_field()函数回显该字段的内容,其格式根据“自定义字段组”管理区域中的参数进行设置。 它可以使用与get_field()相同的$post->ID'option'参数。 它与echo get_field()基本相同。

if ( get_field('staff_image') ) :

子场 (Subfields)

If the custom field is in a Repeater Field or Flexible Content Field, use get_sub_field() and the_sub_field() instead. The $post-ID and 'option' parameters work the same way.


遍历字段 (Looping Through Fields)

To loop through all the rows in a Repeater Field or Flexible Content Field, use have_rows('field_name', $post->ID, 'option') and the_row(). These functions work very similarly to the WordPress loop functions:

要遍历Repeater字段或Flexible Content字段中的所有行,请使用have_rows('field_name', $post->ID, 'option')the_row() 。 这些功能与WordPress循环功能非常相似:

if ( have_rows('image_rows') ) :
	while ( have_rows('image_rows') ) : the_row();

The Flexible Content Field has multiple layouts. The function get_row_layout() returns the name of the layout in use at that iteration of the loop.

灵活内容字段具有多种布局。 函数get_row_layout()返回在该循环迭代中使用的布局名称。

结论 (Conclusion)

When it comes to using Advanced Custom Fields to make WordPress a highly-configurable CMS, the sky is the limit. These fields can be used to create precise custom content input and style to create complex custom layouts. Share one of your best ACF-enabled projects in the comments, or ask a question about using these fields in one of your sites.

当涉及到使用“高级自定义字段”使WordPress成为可高度配置的CMS时,便无所不能。 这些字段可用于创建精确的自定义内容输入和样式,以创建复杂的自定义布局。 在评论中分享您的最佳支持ACF的项目之一,或询问有关在您的一个站点中使用这些字段的问题。

翻译自: https://www.sitepoint.com/getting-started-with-advanced-custom-fields/


参与评论 您还未登录,请先 登录 后发表或查看评论


  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
钱包余额 0