mwdelaney / acf-flexible-content-blocks
为WordPress ACF 5 Pro内容开发提供的一组实用、可重用的灵活内容块。包含模板并自动加载。
Requires
- php: >=5.3.2
This package is auto-updated.
Last update: 2021-09-22 19:15:28 UTC
README
注意: 此插件还在不断开发中。目前包含少量布局,随着时间的推移将包括更多。
一个WordPress插件,提供ACF Pro 5的一系列实用、可重用的灵活内容块。包含基本模板并自动加载,可以在主题级别可选覆盖。
此插件在页面内容编辑器下方创建一个灵活内容字段,并自动在页面模板中 the_content()
下方包含该处输入的内容。
还用一些基本的HTML包装 the_content()
,以便于区分添加的内容块。
需求
- WordPress 4.5+
- 高级自定义字段 Pro 5
使用方法
默认情况下,所有包含的布局都可用
- 内容
- 媒体(图片、oembed、'内容'、代码)
- 带媒体的内容
- 特色内容
- 滑块
- 标签页
- 画廊
- 可折叠
- 卡片
- 文章列表
- Strap
仅启用某些布局
要从可用列表中删除布局,声明仅对您希望使用的布局支持主题
add_theme_support( 'flexible-content-blocks', array( 'content', 'content-with-media' ) );
更改显示块的类型
默认情况下,内容块在页面中启用,要定义哪些文章类型块应在其中可用,请声明主题支持
$landing_page_templates = array(
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
array (
'param' => 'page_template',
'operator' => '!=',
'value' => 'template-no-header-image.php',
),
),
);
add_theme_support( 'flexible-content-location', $landing_page_templates );
模板
每个布局都包含基本模板。这些模板设计用于通过您的主题进行简单的样式化。要覆盖包含的模板,将您希望覆盖的模板(s)从 templates
复制到您的主题中的子目录 fcb-templates
。
基本模板包装
可以根据每个块类型覆盖布局基本。当块渲染时,插件将首先在您的主题中查找,然后查找插件目录中的 layout-base-[layout_name].php
(例如 layout-base-content_with_media.php
),如果不存在特定的基本模板,则插件将加载 layout-base.php
模板。
布局特定模板部分
类似于基本模板,每个模板部分的文件名可以附加一个布局名称(例如,使用 content_with_media
覆盖特定布局的模板)。例如,[your-theme]/fcb-templates/blocks/parts/block-cta-content_with_media.php
将仅在“带媒体的内容”布局中的调用操作中加载。
操作和过滤器
有几个过滤器可用于更改插件输出。
过滤器:fcb_bg_colors
更改或添加块可用“主题”背景颜色
add_filter( 'fcb_bg_colors', 'custom_bg_colors');
function custom_bg_colors($array) {
$array['secondary'] = 'Secondary';
return $array;
}
过滤器:fcb_btn_colors
更改或添加块调用操作的可用按钮颜色
add_filter( 'fcb_btn_colors', 'custom_btn_colors');
function custom_btn_colors($array) {
$array['secondary'] = 'Secondary';
return $array;
}
过滤器:fcb_set_block_htag
设置块标题包裹的标签。默认为<h2>
。首先删除现有的过滤器,然后添加自己的
/**
* Make the first block title an h1 and subsequent blocks default to h2
*/
remove_filter( 'fcb_set_block_htag', 'block_htag_level', 10 );
add_filter( 'fcb_set_block_htag', 'custom_htag_level', 10, 2 );
function custom_htag_level($title, $htag) {
if($GLOBALS['fcb_rows_count'] == 0) {
$htag = 'h1';
} else {
$htag = 'h2';
}
return '<' . $htag . '>' . $title . '</' . $htag . '>';
}
过滤器:fcb_set_block_wrapper_classes
设置应用于内容块包装器的类。此过滤器在每次渲染块时运行,因此可以根据每个块条件性地应用类。
/**
* Give the first block an additional class of 'block-first'
*/
add_filter( 'fcb_set_block_wrapper_classes', 'custom_block_wrapper_classes' );
function custom_block_wrapper_classes($classes) {
if($GLOBALS['fcb_rows_count'] == 0) {
$classes[] = 'block-wrapper-first';
}
return $classes;
}
过滤器:fcb_set_block_classes
设置应用于内容块的类。此过滤器在每次渲染块时运行,因此可以根据每个块条件性地应用类。
/**
* Give the first block an additional class of 'block-first'
*/
add_filter( 'fcb_set_block_classes', 'custom_block_classes' );
function custom_block_classes($classes) {
if($GLOBALS['fcb_rows_count'] == 0) {
$classes[] = 'block-first';
}
return $classes;
}
过滤器:fcb_set_block_wrapper_styles
设置应用于内容块的样式。此过滤器在每次渲染块时运行,因此可以根据每个块条件性地应用样式。
不建议使用此过滤器——它用于插件应用在块中设置的背景样式。语义样式始终优于每个块应用的风格属性。
/**
* Give the first block an ugly green border
*/
add_filter( 'fcb_set_block_wrapper_styles', 'custom_block_styles' );
function custom_block_styles($styles) {
if($GLOBALS['fcb_rows_count'] == 0) {
$styles[] = 'border: 1px solid green;';
}
return $styles;
}
过滤器:fcb_content_before
默认情况下,此插件为WordPress主要内容(the_content()
)提供防护。可以使用此过滤器修改the_content()
的防护和输出。
**
* Add "Contact Us" button before the_content() on landing pages
*/
add_filter( 'fcb_content_before', 'contact_us_button', 10 );
function contact_us_button( $content ) {
if ( on_landing_page() && !empty($content)) {
// Add image to the beginning of each page
$content = sprintf(
'<aside class="btn-content-cta-wrap"><a class="btn btn-content-cta" href="#talk">%s</a></aside>%s',
"Contact Us",
$content
);
}
// Returns the content.
return $content;
}
过滤器:fcb_content_after
默认情况下,此插件为WordPress主要内容(the_content()
)提供防护。可以使用此过滤器修改the_content()
的防护和输出。
**
* Add "Contact Us" button after the_content() on landing pages
*/
add_filter( 'fcb_content_after', 'contact_us_button', 10 );
function contact_us_button( $content ) {
if ( on_landing_page() && !empty($content)) {
// Add image to the beginning of each page
$content = sprintf(
'<aside class="btn-content-cta-wrap"><a class="btn btn-content-cta" href="#talk">%s</a></aside>%s',
"Contact Us",
$content
);
}
// Returns the content.
return $content;
}
过滤器:fcb_get_layouts
更改插件查看布局的PHP类。请参阅添加您自己的布局。
添加您自己的布局
您可以使用过滤器将您自己的布局添加到包含的集合中
/**
* Add layouts to ACFFCB
*/
function my_layouts() {
// The name of the class we keep our layouts in
return 'myLayouts';
}
add_action( 'acf/init', function() {
// Remove the plugin's set default set of layouts
remove_filter ('fcb_get_layouts', 'fcb_layouts_class', 99, 2);
// Add my own layouts
add_filter ('fcb_get_layouts', 'my_layouts', 99, 2);
});
// Extend the original class to include all the default layouts as well as the ones we're adding here.
class myLayouts extends MWD\ACFFCB\Layouts {
/**
*
* Flexible Content Field: Call to Action
*
* @author Michael W. Delaney
*
*/
function feature() {
$FCBFields = new MWD\ACFFCB\Fields(__FUNCTION__);
$FCBRepeaters = new MWD\ACFFCB\Repeaters(__FUNCTION__);
$FCBFlexibleContent = new MWD\ACFFCB\FlexibleContent(__FUNCTION__);
return(
array ( 'order' => '1000',
'layout' => array (
'key' => __FUNCTION__,
'name' => 'call_to_action',
'label' => 'Call to Action',
'display' => 'block',
'sub_fields' => array (
// Call to Action
$FCBFields->tab_cta(),
$FCBFlexibleContent->cta(),
// Tab Endpoint
$FCBFields->tab_endpoint(),
)
)
)
);
}
}