WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get ID of parent category (6 posts)

  1. gongbaojiding
    Member
    Posted 2 years ago #

    I'm trying to get the parent ID of a category. I've searched around but the only things I can find on this get the top level parent ID not the direct parent of the current page ID.

    I have 3 layers of category:

    -cat 1
    -------cat 2
    -------------cat 3

    cat 3 is a child of cat 2, cat 2 is a child of cat 1.

    If I'm currently on the cat 3 page how can I get the ID of the category directly above it? (cat 2)

    Appreciate any help!

  2. irfan_np
    Member
    Posted 2 years ago #

    Try using get_category_parents()
    <?php echo get_category_parents(category, display link, separator, nice name); ?>
    http://codex.wordpress.org/Function_Reference/get_category_parents

    Irfan

  3. gongbaojiding
    Member
    Posted 2 years ago #

    Thanks for the help, however that just displays a list of all the parent category names.

    I just want to get the ID of the parent category directly above.

  4. Big Bagel
    Member
    Posted 2 years ago #

    Going by the function reference for get_the_category() something like:

    $category = get_the_category();
    $parent_cat = $category->category_parent;

    might work.

  5. irfan_np
    Member
    Posted 2 years ago #

  6. gongbaojiding
    Member
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.