WordPress如何实现自动修改文章中H标签格式?

无评论

作者照片

By 霜天

WordPress网站文章需要自动修改文章中H标签的html格式,有的人喜欢用H2标签定义文章中的标题,而有些人喜欢使用H3标签,那么WordPress如何实现自动修改文章中H标签格式?

添加相关功能代码,想要实现这个功能添加代码是必不可少的,下面我就给大家把代码贴在下面,代码量也不大,使用非常方便。

/**

代码功能:自动修改文章中H标签的格式

**/

function chenxingweb_article_h($content) {

$matches = array();

$h_2 = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”/<h2>([^<]+)<\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/h2>/im\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

$h_3 = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”/<h3>([^<]+)<\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/h3>/im\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

//扩展代码放置位置1

if(is_singular() && preg_match_all($h_2, $content, $matches)) {

foreach($matches[1] as $num => $title) {

$title = trim(strip_tags($title));

$content = str_replace($matches[0][$num], \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'<h2><i class=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”icon-unlink\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”></i>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$title.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</h2>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, $content);

}

}

if(is_singular() && preg_match_all($h_3, $content, $matches)) {

foreach($matches[1] as $num => $title) {

$title2 = trim(strip_tags($title));

$content = str_replace($matches[0][$num], \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'<h3><span>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$title2.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</span></h3>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, $content);

}

}

//扩展代码放置位置2

return $content;

}

add_filter( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’the_content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’chenxingweb_article_h\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

把上面的代码粘贴到主题functions.php文件中即可,上面的标签请根据自己网站的情况自由修改。上面我标注了“扩展位置1”和“扩展位置2”如果你需要判断更多标签的话可以复制扩展位置上面的代码然后修改标签类即可。

发表评论