I want my plugin to recognize post's categories before post is loaded. i wrote this code but it works in category page not in post's page with the category is specified from me.

function checkUser() {
    if(is_category('programming')){//it doesn't work when post with "programming" category is being displayed
            echo "Bingo!";
        include_once ('TEMPLATEPATH/get-messages.php');
add_action('template_redirect', 'checkUser');

is_single() is intend to work with post types, not categories.

To check for category you should use is_category('programming').

Note that is_category works on category archives, not on single view.

So, if you want to check in the current is a single view and has the category 'programming' you need the has_term tag:

if( is_single() && has_term('programming', 'category', get_queried_object()) ) {
  include_once ('TEMPLATEPATH/get-messages.php');

See is_single, is_category and has_term on Codex.

Also have a look on how conditional tags works in WP.

  • Oh sorry I've forgotten I wrote is_category('programming') but it works when i click on programming category not in post with 'programming' catgory. (let me edit my question) – user1695063 Sep 7 '13 at 6:14
  • for example I've a 'Qt' post with 'programming'category and with this code it will be displayed when you click on it – user1695063 Sep 7 '13 at 6:18
  • @user1695063 see the edit – gmazzap Sep 7 '13 at 6:20
  • you're welcome @user1695063 – gmazzap Sep 7 '13 at 6:42

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.