Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have several post formats and I want to use add_filter selectively, applying the filter to some of the post formats but not all. When I do this in my functions.php file it affect all the post formats. It also affects all my custom post types which I want to avoid.

function test_filter($content) {
    $content = "test" . $content;
    return $content;
}
add_filter('the_content','test_filter');
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Use get_post_format():

function test_filter($content) {

    $format = get_post_format();

    if ( ! $format )
        return $content;

    if ( 'audio' === $format )
        // do something with audio

    if ( 'aside' === $format )
        // do something with aside

    return "$content <hr>post format: $format";
}

Since the_content() requires a global $post object to work, get_post_format() will always work. It will return FALSE if the current post type does not support post formats and a post format slug otherwise.

share|improve this answer
    
I will try that but I'm a bit confused about how I determine which post formats the filter operates. I also don't understand why you have <hr>post format: in there –  byronyasgur Apr 17 '13 at 16:39
    
The extra HTML is just there to show some useful output. I made an update with better examples. –  toscho Apr 17 '13 at 16:40
    
Thanks. That makes sense. –  byronyasgur Apr 17 '13 at 16:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.