在指定的页面中阻止WordPress插入额外换行

众所周知WordPress会对编辑器里的内容再格式化一遍,比如自动分段。但有些时候这些添加的格式反而也会让人很头疼。最近遇到WordPressinput,select,pre前会插入额外的换行即<br>,从而破坏页面样式的问题。

解决方法为在对应主题的functions.php中增加如下代码:

<?php
/*
 * Description: Disable wpautop on posts/pages with custom field 'wpautop' == false.
 */

function custom_wpautop($content) {
  if (get_post_meta(get_the_ID(), 'wpautop', true) == 'false')
    return $content;
  else
    return wpautop($content);
}

remove_filter('the_content', 'wpautop');
add_filter('the_content', 'custom_wpautop');

remove_filter ('comment_text', 'wpautop');
add_filter('comment_text', 'custom_wpautop');

/*tinyMCE*/
function disable_tinymce_autop($initArray){
   if (get_post_meta(get_the_ID(), 'wpautop', true) == 'false'){
     $initArray['wpautop'] = false;
     $initArray['forced_root_block'] = '';
     $initArray['force_br_newlines'] = TRUE;
     $initArray['force_p_newlines'] = FALSE;
   }
   return $initArray;
}
add_filter('tiny_mce_before_init', 'disable_tinymce_autop');
?>

然后在需要禁止自动格式化的文章中,按照如下图示步骤增加"自定义栏目"

之后,向下滚动页面,在页面编辑的最下面:

添加如下图所示的内容:

添加后的结果如下:

注意,如果安装了Crayon Syntax Highlighter插件,并且启用了插件对于precode标签的拦截,则可能会导致上面的设置无效,其中的换行,其实是Crayon Syntax Highlighter插件引入的。一般我们是要求Crayon Syntax Highlighter插件拦截pre标签,而不拦截code标签。

参考链接


发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注