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