Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying code in Wordpress functions.php to only one category

Tags:

php

wordpress

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!

like image 956
coryetzkorn Avatar asked Jul 18 '11 02:07

coryetzkorn


1 Answers

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.

like image 142
undefined Avatar answered Oct 06 '22 01:10

undefined