虽然目前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,代码有注释。
本站使用静态缓存,登录后获取实时最新资源下载。推荐使用QQ登录,会自动完成注册。
如遇夸克/网盘资源失效,请发帖反馈。站长会积极补链,同时在您的帖子下方评论告知。
如遇夸克/网盘资源失效,请发帖反馈。站长会积极补链,同时在您的帖子下方评论告知。
© 版权声明
资源来源于互联网,供学习交流。如涉侵权,请邮件联系,将予七日内处理。
请在下载后24小时内删除,切勿商用。使用者需自行承担相应法律责任,发布者概不负责。
请在下载后24小时内删除,切勿商用。使用者需自行承担相应法律责任,发布者概不负责。
THE END







![[一键安装合集] 200+款经典游戏夸克盘资源汇总-七玩网](http://static.527wan.top/wp-content/uploads/2026/01/8c26d3722720260127195302.png)

![[一键安装] 武林外传单机版(书生端Pro 2026)最全工具补丁合集|GM工具+小蜜蜂+PCK工具-七玩网](http://static.527wan.top/wp-content/uploads/replace/2026/01/b213f174f9b7e91655c2521209eef22e.jpeg)
![[一键安装] 2024.4逍遥梦幻·花好月圆 三经脉仿官第九版|全新精修迭代版-七玩网](http://static.527wan.top/wp-content/uploads/replace/2026/01/1daf87e24add2e54c5f35dbba849e710.jpeg)


暂无评论内容