Is there a way to limit the following code that I've added to my functions.php file so that it only applies to a single wordpress category?
<?php remove_filter('the_content', 'wpautop'); ?>
I tried this, but it didn't seem to work:
<?php if (in_category('work')) { remove_filter('the_content', 'wpautop'); } ?>
I should also add that I solved this problem by placing the code directly in the specific category template, but I'd prefer to keep the filter in my functions file.
Thanks!
I believe you will want to hook into the pre_get_posts
action. It fires right after the query string is parsed. Some but not all of the conditionals are set up. You can test if in_category() is one of them, but I don't think it matters. Why? Glad you asked.
The hook is going to pass you the query object, which has the property category_name
. All you have to do is check if it has your category and if so fire your filter. Something like this:
function ns_function_name($wpq){
if($wpq->category_name == 'work'){
remove_filter('the_content', 'wpautop');
}
}
add_action( 'pre_get_posts', 'ns_function_name' );
This is totally untested. But since you seem to know what you are doing, it should be enough to put you on the right path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With