Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Code to de-apply wpautop to a specific category of posts WordPress

  • SOLVED

Hi all

I have this code in my functions.php to deactivate the auto-insertion of break/para tags in post content:

// Prevent WP from adding <p> tags on posts
function disable_wp_auto_p( $content ) {
if ( is_singular( 'post' ) ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
return $content;
}
add_filter( 'the_content', 'disable_wp_auto_p', 0 );


However, this applies to all posts, regardless of category. The problem I have that warranted this code in the first place only applies to one category, and I only want the code above to apply to that category. I don't want it to apply to other categories because wpautop works well for those other categories.

Does anyone know how to amend this code so that it only applies to a specific category please?

Thanks
Richard

Answers (3)

2019-09-19

timDesain Nanang answers:

add https://codex.wordpress.org/Conditional_Tags

function disable_wp_auto_p( $content ) {
if ( is_singular( 'post' ) AND in_category( array( 1,2,3 ) ) ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
return $content;
}
add_filter( 'the_content', 'disable_wp_auto_p', 0 );

2019-09-19

Arnav Joy answers:

You can try this



// Prevent WP from adding <p> tags on posts
function disable_wp_auto_p( $content ) {
if ( is_singular( 'post' ) ) {
if ( has_term( 'xxx','category' ) ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
}
return $content;
}
add_filter( 'the_content', 'disable_wp_auto_p', 0 );


You can replace xxx by the actual category slug

2019-09-19

Darlene Grace Arcenal answers:

try this one

function disable_wp_auto_p( $content ) {
if ( in_category( 'category_name' ) && is_singular( 'post' ) ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
return $content;
}
add_filter( 'the_content', 'disable_wp_auto_p', 0 );

where category_name is the slug of the category


where category_name is the slug of category