Support » Plugin: Business Directory Plugin » Get category ID from current category page

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author businessdirectoryplugin

    (@businessdirectoryplugin)

    Hi Alex,

    We use standard WordPress categories for our custom post type, have you tried that API to get it?

    Yes. get_cat_ID($wpbdm_category); is always 0.

    To illustrate what I did. I took the URL, for example http://bla.foo/business-directory/wpbdm-category/category/ and extracted the category part via str_replace. For example, for http://bla.foo/business-directory/wpbdm-category/games/ I get games as the category name. If I try a get_cat_ID(‘games’), I get 0. This happens for every wpbdm category. Where is my mistake?

    Plugin Author businessdirectoryplugin

    (@businessdirectoryplugin)

    Have you tried the_category() (passing in an empty/null post_id?) That gives you the category ID of the current page or post.

    http://codex.wordpress.org/Function_Reference/the_category

    Do you mean <?php the_category() ?> ? The output unfortunately is nothing. Might be because I am on a category page… Oh, and btw, why is there a [resolved]? It’s not resolved…

    Plugin Author businessdirectoryplugin

    (@businessdirectoryplugin)

    I mark threads as resolved when I answer them. It helps me know when there are new responses. But don’t worry, marking it as resolved doesn’t mean I don’t check it.

    I did mean that. If that’s not working, I need to ask my developer what to use instead.

    Yes, unfortunately <?php the_category() ?> isn’t working. It doesn’t give any output, even not zero. It would be great, if you could ask your developer. Thanks a lot. 🙂

    Plugin Author businessdirectoryplugin

    (@businessdirectoryplugin)

    Hi Rodorm,

    My developer replied. Here’s what he said:


    Standard WP functions are allowed inside the loop. This means when used inside excerpt or single view templates.
    Stuff like the_category() will give you the current post’s category if you use it in our listing templates, but outside of them (like in the category page before or after the loop of posts) we are running outside the loop so this is not possible.

    It’d be nice to know exactly what the user is trying to achieve here since we don’t allow the category template as a whole to be customized, but a solution (at least until more details are available) would be what he tried to do before: extracting the slug from the URL. This didn’t work for him because get_cat_ID is for WordPress post categories (not custom taxonomies) only and because it uses the category name (not the slug). A better approach is to use get_term_by (see http://codex.wordpress.org/Function_Reference/get_term_by). Something like get_term_by(‘slug’, ‘games’, wpbdp_categories_taxonomy()) should work for him.

    Thanks for the reply. I solved it differently. I extracted the category ID of the wpbdp category via a normal MySQL query on post_meta.

    Plugin Author businessdirectoryplugin

    (@businessdirectoryplugin)

    That works too.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Get category ID from current category page’ is closed to new replies.