微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

禁止 WordPress 自动添加分段标签p

站长朋友们在使用wordpress建站的过程中会发现,wordpress 本身有个自动分段机制,只要你在可视化模式下敲击一次回车(文本模式敲击两次回车),就会在前台html自动添加分段标签p,其实这个功能是非常方便的,但是在某些情况下干扰了文章内容的格式。

这个自动分段机制,使用的是 wpautop() 函数,只会对文章内容摘要生效,如果您希望禁用它,可以将下面的代码添加主题的 functions.PHP 文件即可:

Crayon-5c891cc8f2ff4678108371" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc8f2ff4678108371-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc8f2ff4678108371-2">2
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc8f2ff4678108371-1">Crayon-e">remove_filterCrayon-sy">(Crayon-h"> Crayon-s">'the_content'Crayon-sy">,Crayon-h"> Crayon-s">'wpautop'Crayon-h"> Crayon-sy">)Crayon-sy">;Crayon-h"> Crayon-c">//正文
Crayon-line Crayon-striped-line" id="Crayon-5c891cc8f2ff4678108371-2">Crayon-e">remove_filterCrayon-sy">(Crayon-h"> Crayon-s">'the_excerpt'Crayon-sy">,Crayon-h"> Crayon-s">'wpautop'Crayon-h"> Crayon-sy">)Crayon-sy">;Crayon-h"> Crayon-c">//摘要

但是这样一来,就会使所有文章类型文章都失去自动分段功能,要解决这个问题,我们可以通过条件标签来判断,在主题的 functions.PHP 添加下面的代码

Crayon-5c891cc8f2ffe906824622" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc8f2ffe906824622-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc8f2ffe906824622-2">2
Crayon-num" data-line="Crayon-5c891cc8f2ffe906824622-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc8f2ffe906824622-4">4
Crayon-num" data-line="Crayon-5c891cc8f2ffe906824622-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc8f2ffe906824622-6">6
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc8f2ffe906824622-1">Crayon-t">functionCrayon-h"> Crayon-e">needRemovePCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891cc8f2ffe906824622-2">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-e">get_post_typeCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-o">==Crayon-h"> Crayon-s">'post'Crayon-sy">)Crayon-sy">{Crayon-h"> Crayon-c">// 如果文章类型为 post
Crayon-line" id="Crayon-5c891cc8f2ffe906824622-3">Crayon-h"> Crayon-e">remove_filterCrayon-sy">(Crayon-s">'the_content'Crayon-sy">,Crayon-h"> Crayon-s">'wpautop'Crayon-sy">)Crayon-sy">;Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891cc8f2ffe906824622-4">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891cc8f2ffe906824622-5">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891cc8f2ffe906824622-6">Crayon-e">add_actionCrayon-h"> Crayon-sy">(Crayon-s">'loop_start'Crayon-sy">,Crayon-h"> Crayon-s">'needRemoveP'Crayon-sy">)Crayon-sy">;

注:以上代码的第 2 行就限定了文章类型为 post 的文章才会取消自动分段,您可以根据自己的需要修改文章类型

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐