WordPress.org

Ready to get started?Download WordPress

Forums

Business Directory Plugin
[resolved] Get category ID from current category page (11 posts)

  1. Rodorm
    Member
    Posted 1 year ago #

    Hi all,

    is there an easy way to get the category ID from the current category page http://bla.foo/business-directory/wpbdm-category/category/ via PHP?

    Alex

    http://wordpress.org/extend/plugins/business-directory-plugin/

  2. businessdirectoryplugin
    Member
    Plugin Author

    Posted 1 year ago #

    Hi Alex,

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

  3. Rodorm
    Member
    Posted 1 year ago #

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

  4. Rodorm
    Member
    Posted 1 year ago #

    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?

  5. businessdirectoryplugin
    Member
    Plugin Author

    Posted 1 year ago #

    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

  6. Rodorm
    Member
    Posted 1 year ago #

    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...

  7. businessdirectoryplugin
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  8. Rodorm
    Member
    Posted 1 year ago #

    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. :-)

  9. businessdirectoryplugin
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  10. Rodorm
    Member
    Posted 1 year ago #

    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.

  11. businessdirectoryplugin
    Member
    Plugin Author

    Posted 1 year ago #

    That works too.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.