WordPress.org

Ready to get started?Download WordPress

Forums

understanding get_the_category(), multiple cats, and parents (3 posts)

  1. ultrus
    Member
    Posted 6 years ago #

    Hello,
    I'm creating a breadcrumb script, and need to understand how to get parent category IDs and names of a category and post. This seems to get complicated as a post can be in multiple categories, including the parent category itself.

    For discussion, let's say I have the following categories:

    12 - Beastly Cat
    20 -- Ratty Cat
    21 -- Black Cat
    22 -- Runt Cat

    Beastly Cat, Ratty Cat, Black Cat, and Runt Cat all share a single post titled, "Scratch Post".

    If I'm at "Scratch Post", get_the_category($cat) (once I get $cat) returns an array of all the cats. This makes sense, but may cause challenges with my breadcrumb scheme.

    If I'm at /?cat=12, get_the_category($cat) returns an array of all the cats as well. Eh? I think this is because the page is listing "Scratch Post" on the page. However, I just want info for the current category and eventually any parent cats that cat belongs to.

    Would I do a custom database query for this? Would I treat is_single scripts different than is_category scripts? How would I get started?

    Thanks for the assist. :)

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    You're confused about what get_the_category() does.

    get_the_category() doesn't take a category as a parameter. It takes a post ID as a parameter. It then returns all the categories for that post. Without any parameter, it returns info about the current post.

    If you want information about a specific category, you should be using get_category($cat). It takes the category as a parameter and returns info about that one category only. The $cat parameter is not optional here.

  3. ultrus
    Member
    Posted 6 years ago #

    Oh! That should set me straight. Thanks! :)

Topic Closed

This topic has been closed to new replies.

About this Topic