5

I'm familiar with this little trick already for removing auto paragraph formatting in WordPress:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

...however adding this in functions.php removes paragraphs for the entire site. This is not what I want since the client needs to be able to make edits themselves (the paragraph formatting really helps them out on posts).

Where the auto paragraph insertions are particularly damaging is on the client's home page, where there are javascript snippets. So ideally, I would like to disable auto p formatting for this page alone, or all pages if necessary, and leave posts alone.

Any ideas? I can provide more information if needed.

Thanks in advance!


Edit:

Plugins I've tried: Php Exec, Raw HTML, Disable WordPress Autop, PS Disable Auto Formatting, Toggle wpautop

4

2 回答 2

13

您应该能够使用is_page()检查正在呈现的模板是否是页面,然后可以选择运行过滤器。我们挂钩'wp_head',以便我们可以在the_content调用之前运行检查。

例子:

function remove_p_on_pages() {
    if ( is_page() ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );
    }
}
add_action( 'wp_head', 'remove_p_on_pages' );
于 2014-10-26T08:02:42.060 回答
0

您可以将自定义类别添加到所需页面,然后使用类别 ID 禁用 wp_autop

//no paragraph
function no_auto_paragraph( $atts ){

  $cats = get_the_category();
  $cat  = $cats[0]->cat_ID; 

  if ($cat == 7 ){ //in my case the category is 7
    remove_filter( 'the_content', 'wpautop' );
  }

}

add_action( 'wp_head', 'no_auto_paragraph' );

//no_auto_paragraph END
于 2017-01-04T03:54:41.177 回答