【发布时间】:2020-05-31 11:11:22
【问题描述】:
一段时间以来,我一直在尝试通过在子主题的 functions.php 文件中放置一个函数来为帖子标题添加 ID 标记。
我已经尝试了下面的代码,该代码适用于在帖子内容中包含任何没有 ID 的 h1 到 h6 的 ID(并留下任何单独的 ID) - 但我找不到方法为实际显示的帖子标题添加 ID?
function auto_id_headings( $content ) {
$content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) {
if ( ! stripos( $matches[0], 'id=' ) ) :
$matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $matches[3] . $matches[4];
endif;
return $matches[0];
}, $content );
return $content;
}
add_filter( 'the_content', 'auto_id_headings' );
我认为这就像将代码的“add_filter”部分中的the_content 更改为the_title 一样简单,但这并没有奏效!仅供参考,我正在尝试执行此操作的页面在这里:https://www.futureproofpromotions.com/reaction-form/
有人可以帮忙吗?
我已经包含了该页面的 chrome 开发人员工具的屏幕截图,解释了我在下面尝试实现的目标:
【问题讨论】: