虽然目前WordPress区块编辑器,可以在正文中添加任意区块,包括区块小工具,但想在所有文章中添加一个固定的内容,每次还是需要手动添加区块,文本分享一段代码,可实现在WordPress文章中添加一个固定的,与正常侧边栏一样的小工具,方便使用。
将下面代码添加到当前主题函数模板functions.php中:
// 添加小工具
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => '正文小工具',
'id' => 'inline-content',
'description' => '用于在正文中添加小工具',
'before_widget' => '<aside id="%1$s" class="widget inline-content %2$s">',
'after_widget' => '<div class="clear"></div></aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
// 添加到正文第二个段落下面,修改数字2可调整位置
add_filter( 'the_content', 'insert_content_filter' );
function insert_content_filter( $content ) {
ob_start();
$sidebar = dynamic_sidebar('inline-content');
$new_content = ob_get_clean();
if ( is_single() && ! is_admin() ) {
return insert_content( $new_content, 2, $content );
}
return $content;
}
// 添加到正文段落中
function insert_content( $new_content, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $new_content;
}
}
return implode( '', $paragraphs );
}
之后进入小工具设置页面会发现新增“正文小工具”,与正常侧边栏操作一样添加小工具。
最后可以针对自己的主题适当给这个小工具添加样式:
.inline-content {
border: 1px solid #666;
}
默认是添加到正文第二个段落下面,可以根据情况调整小工具插入位置,修改数字2,代码有注释。
如遇夸克/网盘资源失效,请发帖反馈。站长会积极补链,同时在您的帖子下方评论告知。
© 版权声明
资源来源于互联网,供学习交流。如涉侵权,请邮件联系,将予七日内处理。
请在下载后24小时内删除,切勿商用。使用者需自行承担相应法律责任,发布者概不负责。
请在下载后24小时内删除,切勿商用。使用者需自行承担相应法律责任,发布者概不负责。
THE END



![[一键安装] 原神4.0.1单机版 | 枫丹新角色林尼/夜兰/琳妮特 | WIN一键服务端+GM后台+全卡池-七玩网](http://static.527wan.top/wp-content/uploads/2026/08/dfc3f48dd920260810022231.jpg)

![[一键安装] 天龙八部X64 免虚拟机版 纯Windows 纯断网 免路由器 雪舞编译服务端 V20260804 V0.5-七玩网](http://static.527wan.top/wp-content/uploads/replace/2026/08/270e579df1c8f54d6e0b447ae31e0056.jpeg)
![[一键安装] 2026年7月19日重磅更新!动作单机之魂系某0(原永恒8.4机器人端作者新作BNS)-七玩网](http://static.527wan.top/wp-content/uploads/2026/08/030da0001620260810145333.png)



暂无评论内容