Support » Fixing WordPress » understanding get_the_category(), multiple cats, and parents

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    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.

    Oh! That should set me straight. Thanks! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘understanding get_the_category(), multiple cats, and parents’ is closed to new replies.