0

I would like to achieve a list of sibling categories to the deepest level category (4 or 5 levels deep) of my posts, for example:

Post about South Sudan (breadcrumbs = World, Africa, East Africa, South Sudan)

Display all subcategories to South Sudan.

Then I would like to be able to do the same thing, but with East Africa (1 level up).

Is this even possible on WordPress. Two of us have been working on this problem for days.

If someone could manage to do this, in addition to my real and genuine thanks and appreciation, I could offer something on their wish list up to around $20.

Thank you all

2

Yes, you can. The Function to help you out here is get_term(). The return of this function delivers you a value for parent. Using this ID, you can get the Parent Taxonomy (as long as it is hierarchical like the categories).

Attention

To get this function to work correctly, you have to select JUST the bottom category for your post, for example "South Sudan". If you also select parent Categories, the function has no way of knowing which one to use (or just with a lot more processing).

The same problem arrises if you use multiple categories - either you get the output of multiple breadcrumbs, or just one, which could be the false one.

The script for a single bottom category could look like that, and should give you the idea, what needs to be done:

UPDATE

I enhanced the script with a function to ensure multi-category compatibility. It now searches through all the set categories, finds the one with the most category-ancestors, and prints out a breadcrumb with all the categorylinks.

$seperator = ' | '; // Change to whatever you need
$categories = get_the_category( get_the_ID() ); // get the categories for your post

// check if categories are set
if ( is_array( $categories ) && count( $categories ) > 0 ) {

    $mostancestors = 0; //controlvariable
    $foundcategory; //save the category

    // Loop through the categories
    foreach( $categories as $thiscategory ) {

        //check how many ancestors the current category has
        $thisancestors = howManyAncestors( $thiscategory );
        // if more than the previous record holder
        if ( $thisancestors > $mostancestors ) {
            $mostancestors = $thisancestors; //increase the controlvariable to the Amount calculated
            $foundcategory = $thiscategory; //Set the found category to the one with more ancestors
        }
    }

    // get the parent of the found category
    $parent = get_term( $foundcategory->parent, 'category' );

    // loop to the next parent as long as it is different than the current one and exists
    while ( $parent->term_id != $foundcategory->term_id && !is_wp_error( $parent ) && $parent->term_id != 0 ) {

        // Add the Link for each parent in front of the existing Links
        $categorylinks = '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">' . $parent->name . '</a>' . $seperator . $categorylinks;
        // get the next parent
        $parent = get_term( $parent->parent, 'category' );

    }

    // the actual category that is set, without the final Seperator
    $categorylinks.= '<a href="' . get_category_link( $foundcategory->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $foundcategory->name ) ) . '">' . $foundcategory->name . '</a>';

    echo $categorylinks;

}

function howManyAncestors( $term ) {
    $counter = 0;
    $parent = get_term( $term->parent, 'category' );
    while ( $parent->term_id != $thiscategory->term_id && !is_wp_error( $parent ) && $parent->term_id != 0 ) {
        $counter++;
        $parent = get_term( $parent->parent, 'category' );
    }
    return $counter;
}
  • Hello fischi, thanks for the explanation on get_term(). We had used dozens of script attempts with this function, and now I understand the results: We received multiple subcategory print-outs (some were doubled/quadrupled), probably because it was providing all subcategories for all checked categories. There's no way around us checking multiple categories, though, as we have to organize our posts to belong to wider/narrower sections of the site. What would the "more processing" route entail? – Justin Munce May 27 '15 at 19:11
  • To loop through all your categories, and check which one has the most ancestors (For Example World (0), Africa(1), East Africa(2), South Sudan(3)), so the one with the most is the Category you want to display in the breadcrumb. – fischi May 27 '15 at 19:54
  • Wow. Great adaptive suggestion! How could I grab the one with the least/most ancestors? That could really work. – Justin Munce May 27 '15 at 20:32
  • I think you need to use my above script with a counter for that. You have the array of categories, and looping through the parents (like in my while) increase the counter. Afterwards look for the one with the highest counter. Unfortunately a function like get_category_parents() just returns a string, so you can not use it.. – fischi May 27 '15 at 20:40
  • After testing a lot of code today, I think this is pretty much beyond me. If someone could manage to do this, in addition to my real and genuine thanks and appreciation, I could offer something on their wish list up to around $20. – Justin Munce May 28 '15 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.