utilization Advanced Custom Fields(ACF) plugin, you can easily create custom blocks for Gutenberg without having to deeply master the JavaScript and other advanced techniques. In this article, the details of how to create an ACF via Customizing the call to action(CTA) block, add elements to your website that will attract users to click through.
![图片[1]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225113915997-22c7a9e2b4c581e696034e42ec6f970.jpg)
Why Gutenberg and ACF CreationCustomized blocks?
The Gutenberg editor is available through the "blocs
" concept makes managing and organizing content easier and more modular. By using the ACF A plugin that allows you to create custom blocks in the Gutenberg editor that exactly fit your needs without having to write complex JavaScript code. ACF provides a clean way for developers and designers to easily customize fields through the WordPress backend to create powerful and unique looking blocks.
move
- Register custom blocks: In the WordPress theme's
functions.php
file to register custom blocks. - Creates an ACF field group: Create custom fields containing titles, descriptions, buttons, etc.
- Create a block template: Write a template file using PHP to define the front-end presentation of the block.
- Add custom styles: Style the block with a CSS file so that it matches the design style of your website.
- Using blocks in Gutenberg: Insert the created CTA block into a page or post to get started.
Step 1: In the functions.php
Register custom blocks in the file
First of all, you need to add the functions.php
Register custom blocks in the file (bottom). Customized blocks are registered in the file (bottom) provided by ACF through the acf_register_block_type()
function, it can be very easy to register a new block.
function register_custom_cta_block() {
if (function_exists('acf_register_block_type')) {
acf_register_block_type(array(
'name' => 'custom_cta',
'title' => __('Custom CTA Block'),
'description' => __('A custom call to action block with an image, title, description, and button.'),
'render_template' => 'template-parts/blocks/custom-cta.php',
'category' => 'common',
'icon' => 'megaphone',
'keywords' => array('cta', 'call to action', 'button'),
'enqueue_style' => get_template_directory_uri() . '/template-parts/blocks/custom-cta.css',
));
}
}
add_action('acf/init', 'register_custom_cta_block');
In this code, the following configuration is done:
name
: Sets the unique identifier of the custom block.title
: The display name of the block in the Gutenberg editor.description
: A short description of the function of the block.render_template
: Specifies the path to the PHP template file that renders the content of the block on the frontend.category
: Specifies the classification of the block, determining where it will appear in the Gutenberg editor.icon
: Set up block icons for easy recognition in Gutenberg.keywords
: Set keywords to make it easy for users to search for the block.enqueue_style
: Specifies the path to the block's stylesheet for loading styles.
![图片[2]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225111938850-image.png)
Step 2: Create ACF Field Groups for Blocks
Next, you need to create the ACF field groups by first installing the ACF plugin. This will allow you to fill in the necessary information in the Gutenberg editor, such ascaption,descriptive,Button Text,photographetc.
![图片[3]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225104127301-image.png)
- Log in to the WordPress backend and go to ACF > Field GroupsThe
![图片[4]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225104307865-image.png)
- strike (on the keyboard) Add new field group, create a file named "Customizing CTA Block Fields" for the field group.
![图片[5]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225111443381-image.png)
- Add the following fields to this field group:
- Title (Text): Adds a field to the CTA block for the title.
- Description (Text Area): Adds a text area field to the CTA block for description.
- Image: Adds a picture field to the CTA block.
- Button Text (Text): Adds a button text field to the CTA block.
- Button URL (URL): Add a button link field to the CTA block.
- Background color (Color Picker): Adds a color picker field to the CTA block to set the background color.
![图片[6]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225111341939-image.png)
- exist Positioning rules Set the condition in the rule to "Block equals custom CTA block."The ACF Pro version is required here. This way the ACF fields will only show up if this custom block is used.
![图片[7]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225110655638-image.png)
Step 3: Create a Block Template
Now that the custom block has been registered and the appropriate fields have been created, we next need to create a template file to render the contents of the block.
- In your theme directory, navigate to
template-parts/blocks/
Folder. If the folder does not exist, you need to create one. - Create a new file in this folder named
custom-cta.php
The - exist
custom-cta.php
file, use the following code to output the field contents:
<?php
$title = get_field('title');
$description = get_field('description');
$image = get_field('image');
$button_text = get_field('button_text');
$button_url = get_field('button_url');
$background_color = get_field('background_color');
?>
<div class="custom-cta-block" style="background-color: ">
<?php if ($image): ?>
<img src="" alt="" class="cta-image" />
<?php endif; ?>
<?php if ($title): ?>
<?php endif; ?>
<?php if ($description): ?>
</p
<?php endif; ?>
<?php if ($button_text && $button_url): ?>
<a href="" class="cta-button">
<?php echo esc_html($button_text); ?>
<?php endif; ?>
Step 4: Creating CSS Styles for CTA Blocks
![图片[8]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225113554379-image.png)
To make custom CTA blocks look better, you can add a few CSS styles. In template-parts/blocks/
folder to create a file named custom-cta.css
file and add the following style:
.custom-cta-block {
padding: 40px;
border-radius: 15px;
text-align: center;
color: #fff.
max-width: 700px;
margin: 30px auto;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.custom-cta-block:hover {
transform: translateY(-5px);
}
.cta-image {
max-width: 120px;
margin-bottom: 20px;
border-radius: 50%.
border: 4px solid #fff;
}
.cta-title {
font-size: 28px;
margin: 15px 0;
font-weight: 700;
}
.cta-description {
font-size: 18px;
margin-bottom: 20px;
line-height: 1.5;
}
.cta-button {
display: inline-block;
padding: 12px 25px;
background-color: #ff6b6b;
color: #fff.
text-decoration: none;
border-radius: 25px;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
transition: background-color 0.3s ease;
}
.cta-button:hover {
background-color: #e05656.
}
Step 5: In the Gutenberg EditorUsing custom blocks in
Finally, you can use the custom CTA blocks you created in the Gutenberg editor.
- Open a post or page.
- strike (on the keyboard) + button to add a new block.
![图片[9]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225112528453-image.png)
- look for sth. Customized CTA Blocks and insert it into the content.
![图片[10]-如何使用 ACF 插件为 Gutenberg 创建自定义行动号召(CTA)块](https://www.361sale.com/wp-content/uploads/2024/12/20241225112411985-image.png)
- Fill out the fields in the editor with the title, description, image, button text, button URL, and background color.
- Publish or preview a post to see the effect of a custom CTA block you've created.
summarize
Using the ACF plugin, it's easy to create customized call-to-action (CTA) blocks for the Gutenberg editor without having to write complex JavaScript code. This tutorial details how to register custom blocks in WordPress, create ACF fields, design front-end templates, add styles and use these custom blocks in Gutenberg.
Link to this article:https://www.361sale.com/en/31807The article is copyrighted and must be reproduced with attribution.
No comments