WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Category parent shows after sub-category (6 posts)

  1. crsuperman34
    Member
    Posted 1 year ago #

    Unfortunately my problem exists on my localhost so I can't forward you to the website.

    Check the following screenshots. 'design' is a parent of 'work' same as 'Travel' is a parent of 'New Zealand'
    http://www.pommebelle.com/code_files/images/categories.png
    http://www.pommebelle.com/code_files/images/tags.png

    I'm using this code:
    $category = get_the_category();
    $wut = $category[0]->cat_name;
    to pull the parent category.

    'New Zealand' (or any sub-category of 'travel') gets returned with 0 whereas it should be the parent, 'travel' that gets returned. Is there anyway to change the order? Why is it wrong to begin with?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    to pull the parent category.

    this will pull the category of the post with the lowest category ID;

  3. crsuperman34
    Member
    Posted 1 year ago #

    Right but if I use $category[0]->cat_name; on the design category I get the parent 'design' returned. If I use it on the travel category I get the sub-category returned.

    Both Categories have only posts with equal depth: a parent and a sub. Yet, I get two different results.

    If you look at the screenshot of the admin posts page you see design listed first (before the sub) and Travel listed second (after the sub).

  4. crsuperman34
    Member
    Posted 1 year ago #

    nevermind, I understand. This code doesn't do what I thought it does.
    I needed: get_category_parents($cat, FALSE, '', TRUE)

  5. crsuperman34
    Member
    Posted 1 year ago #

    The sub category is listed first on Travel pages.
    if I use
    $mycategories = get_the_category_list();

    I'll get:
    Sub Category, Parent on the travel category
    and
    Parent, Sub Category on the design category

    I need the list to always return Parent then Sub Category.

  6. crsuperman34
    Member
    Posted 1 year ago #

    $category = get_the_category();
    $parent = get_cat_name($category[0]->category_parent);
    if (!empty($parent)) {
    echo $parent;
    } else {
    echo $category[0]->cat_name;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic