How do I remove auto paragraph formatting for pages ONLY, and not posts (WordPress)

我与影子孤独终老i 提交于 2019-12-05 17:22:18

You should be able to check if the template being rendered is a page using is_page(), and then optionally run the filter. We hook into 'wp_head', so that we can run the check before the_content is called.

Example:

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' );

You can add custom category to desired page then use the category ID to disable 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

I suggest adding it to the theme's home.php file if you must. Ideally, just added it to the javascript file of the theme or otherwise separate the content (the homepage content) from the controller (your javascript) (e.g. including a JS file only on the homepage).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!