0

I am trying to apply wordpress filter hook get_the_expert. My intention is to apply filter only when the category slug is "MY-CATEGORY-SLUG". How can I check the category slug inside the filter hook function.

Thanks

  • The category slug of the current category being viewed (category archive page)? The category of the current post in the loop? – cybmeta Feb 11 '15 at 10:53
  • Yes category of the current post in the loop. – Pramod Sivadas Feb 11 '15 at 11:09
  • Please clarify - by "apply wordpress filter hook", do you mean that you wish to only show the excerpt if a Post is in the category MY-CATEGORY-SLUG? – David Gard Feb 11 '15 at 11:36
1

Thanks for the response David. I found the solution. I added the following code in functions.php

add_filter('get_the_excerpt', 'process_excerpt');

function process_excerpt($param) {
    global $post;
    if(in_category('MY-CATEGORY-SLUG',$post))
        return 'adding some data'.$param;
    else
        return $param;
}

This fixed my issue.

  • Thanks for posting your answer, and glad I helped you find the solution :) – David Gard Feb 11 '15 at 12:04
0

As you are inside the loop and you want to apply the filter only when the current post is assigned to specific category, you could do something like this in the loop:

if( have_posts() ) {
    while( have_posts() ) {

        the_post();

        if( in_category( 'MY-CATEGORY-SLUG', get_the_ID() ) ) {
            //The post is assigned to "MY-CATEGORY-SLUG", add the filter
            add_filter('get_the_excerpt', 'filter_the_excerpt');
        } else {
            //The post is NOT assigned to "MY-CATEGORY-SLUG", remove the filter
            remove_filter('get_the_excerpt', 'filter_the_excerpt');
        }

    }
}

But I think you should apply the filter always and check the category inside the filter, so you can avoid the checking in the loop in order to keep the loop and themplates clean:

add_filter('get_the_excerpt', 'filter_the_excerpt');
function my_filter_the_excerpt( $excerpt ){

    if( in_category( 'MY-CATEGORY-SLUG', get_the_ID() ) ) {
        //The post is assigned to "MY-CATEGORY-SLUG"
        //Do whatever you want
    } else {
        //The post is NOT assigned to "MY-CATEGORY-SLUG"
        //Do whatever you want
    }


}
0

If I have understood your question correctly, this code below should help you (place it in functions.php).

function my_filter_the_excerpt($excerpt){

    global $post;

    /** Return the excerpt only if the post is in the category 'MY-CATEGORY-SLUG' */
    return (in_category('MY-CATEGORY-SLUG', $post) ? $excerpt : ''; 

}

Just in case you are unsure of where this code should go, I'd recommend addig the filter just before your Loop starts, and then removing it at the end -

add_filter('get_the_excerpt', 'my_filter_the_excerpt');
if(have_posts()) : while(have_posts()) : the_post();

        { your code }

    endwhile;
endif;
remove_filter('get_the_excerpt', 'my_filter_the_excerpt');

For more information, I'd recommend having a look at the following documents -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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