简体   繁体   中英

Having trouble with a function in Wordpress

I have the following problem:

I don't want WP adding wpautop to all pages, but only the ones I need, so I added this:

function my_wpautop_correction() {
    if( is_page() ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );      
    }
    if( is_page( array(79, 81) ) ) {
        add_filter( 'the_content', 'wpautop' );
        add_filter( 'the_excerpt', 'wpautop' );     
    }
}
add_action('pre_get_posts', 'my_wpautop_correction');

Seems to be working fine, but I am not sure if its the best way to write that function. I've tried this:

function my_wpautop_correction() {
    if( !( is_page(79) || is_page(81) ) ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );      
    }
}
add_action('pre_get_posts', 'my_wpautop_correction');

But it doesnt work, what I am doing wrong? I want to add wpautop only to pages 79 and 81.

Try this:

function my_wpautop_correction() {
    if( !is_page(79) || !is_page(81) ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );      
    }
}
add_action('pre_get_posts', 'my_wpautop_correction');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM