
7折
减价出售
¥799
在WordPress块主题时代,自定义块已成为扩展功能的核心方式。不同于传统主题的短代码和自定义字段,块编辑器提供了一整套完整的交互式组件开发生态。通过创建自定义块,开发者可以构建与古腾堡编辑器无缝集成的丰富内容元素。
一个典型的自定义块包含以下文件结构:
my-custom-blocks/
├── src/
│ ├── blocks/
│ │ └── my-first-block/
│ │ ├── edit.js
│ │ ├── save.js
│ │ ├── index.js
│ │ └── style.scss
│ └── init.php
├── build/
├── package.json
└── my-custom-blocks.php
创建一个带样式控制的文本区块:
// src/blocks/styled-text/index.js
import { registerBlockType } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';
registerBlockType('my-plugin/styled-text', {
title: 'Styled Text',
icon: 'editor-textcolor',
category: 'text',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'p'
},
textColor: {
type: 'string'
}
},
edit: ({ attributes, setAttributes }) => (
<RichText
tagName="p"
style={{ color: attributes.textColor }}
value={ attributes.content }
onChange={ (content) => setAttributes({ content }) }
/>
),
save: ({ attributes }) => (
<RichText.Content
tagName="p"
style={{ color: attributes.textColor }}
value={ attributes.content }
/>
)
});
创建从API获取数据的动态块:
// src/blocks/weather-widget/index.js
import { registerBlockType } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';
registerBlockType('my-plugin/weather', {
title: 'Weather Widget',
icon: 'cloud',
category: 'widgets',
attributes: {
location: { type: 'string', default: 'New York' },
unit: { type: 'string', default: 'celsius' }
},
edit: ({ attributes, setAttributes }) => {
const { weather, isLoading } = useSelect((select) => {
const { getWeather } = select('my-plugin/weather');
return {
weather: getWeather(attributes.location),
isLoading: select('my-plugin/weather').isLoading()
};
});
return (
<div className="weather-widget">
<input
type="text"
value={ attributes.location }
onChange={ (e) => setAttributes({ location: e.target.value }) }
/>
{ isLoading ? (
<p>Loading weather data...</p>
) : (
<p>Current temperature: {weather?.temp}°{attributes.unit === 'celsius' ? 'C' : 'F'}</p>
) }
</div>
);
},
save: () => null // 动态块在前端渲染
});
在主题的theme.json
中定义块样式变体:
{
"styles": {
"blocks": {
"my-plugin/styled-text": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--heading)",
"fontWeight": "700"
}
}
}
},
"customTemplates": [
{
"name": "home",
"title": "Home",
"blocks": [
{
"name": "my-plugin/hero",
"settings": {
"backgroundColor": "var(--wp--preset--color--primary)"
}
}
]
}
]
}
在块主题模板中预置自定义块并锁定编辑:
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:my-plugin/hero {"lock":{"move":true,"remove":true}} -->
<div class="wp-block-my-plugin-hero">
<!-- wp:heading {"level":1} -->
<h1>Welcome to our site</h1>
<!-- /wp:heading -->
</div>
<!-- /wp:my-plugin/hero -->
</div>
<!-- /wp:group -->
创建可复用的块组合模式:
// 在主题的functions.php中注册块模式
function register_my_block_patterns() {
register_block_pattern(
'my-theme/featured-content',
array(
'title' => __( 'Featured Content Grid', 'my-theme' ),
'description' => __( 'A grid of featured content with title and excerpt', 'my-theme' ),
'content' => '<!-- wp:columns --><div class="wp-block-columns"><!-- wp:column --><div class="wp-block-column"><!-- wp:my-plugin/content-card {"featured":true} /--></div><!-- /wp:column --></div><!-- /wp:columns -->',
'categories' => array( 'featured' ),
)
);
}
add_action( 'init', 'register_my_block_patterns' );
实现动态块的前端渲染:
// 在插件主文件中注册渲染回调
function register_dynamic_blocks() {
register_block_type( 'my-plugin/weather', array(
'render_callback' => 'render_weather_block',
'attributes' => array(
'location' => array( 'type' => 'string' ),
'unit' => array( 'type' => 'string' )
)
) );
}
add_action( 'init', 'register_dynamic_blocks' );
function render_weather_block( $attributes ) {
$weather_data = get_weather_from_api( $attributes['location'] );
ob_start(); ?>
<div class="weather-widget">
<h3>Weather in <?php echo esc_html( $attributes['location'] ); ?></h3>
<div class="temperature">
<?php echo esc_html( $weather_data['temp'] ); ?>°
<?php echo $attributes['unit'] === 'celsius' ? 'C' : 'F'; ?>
</div>
</div>
<?php
return ob_get_clean();
}
在WordPress主题的宇宙中,块主题如同一座由代码构建的数字乐高城堡。每个自定义块都是独特的积木,开发者则是这座城堡的建筑师和魔术师。现代块开发已经超越了简单的功能实现,演变为一种融合设计思维与技术创新的艺术形式。这种开发模式的转变要求我们以设计师的审美、工程师的精确和艺术家的创造力来对待每一行代码。当技术限制逐渐消失,唯一限制我们的将是想象力本身。在这个新时代,每个开发者都有机会成为数字世界的造物主,用代码绘制互联网的未来图景。
减价出售
减价出售
减价出售
减价出售
电话咨询
1855-626-3292
微信咨询