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

WordPress 有个自动分段机制,只要你在可视化下敲击一次回车(文本模式敲击两次回车),就会在前台html自动添加分段标签<p>,其实这个功能也是很方便的,但是有些朋友就是不习惯,或者在某些情况下干扰了自己的文章内容格式。

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

remove_filter( 'the_content', 'wpautop' ); //正文
remove_filter( 'the_excerpt', 'wpautop' ); //摘要

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

function needRemoveP() {
	remove_filter('the_content', 'wpautop'); 
}

然后在你想去掉功能的地方,比如想让文章去掉这个默认的功能,则在single.php里添加:

&lt;?php add_action ('loop_start', 'needRemoveP'); ?&gt;

倡萌补充:

其实我们还有更加方便的方法,不需要修改 single.php 文件,而是通过条件标签来判断,在主题 functions.php 使用下面的代码:

function needRemoveP() {
	if ( get_post_type() == 'post'){ // 如果文章类型为 post
		remove_filter('the_content', 'wpautop'); 
	}
}
add_action ('loop_start', 'needRemoveP');

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

来源:

https://www.wpdaxue.com/disable-autop.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32726.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?