0

Our website: www.cinemanext.at basically shows the first child category of the category events (click page "events"). Now, page "service" shows category service, which is a child category of events. But service has more levels of child categories.

There is events -> service -> nachwuchsfoerderungen and even one step deeper: events -> service -> nachwuchsfoerderungen -> entwicklung, for example.

Unfortunately, in the grid boxes on the website - the place where the categories are displayed, it always shows the first child category: which is fine for the page "events" (only one level of child categories), but not for the page "service", since it has more than one level of child categories.

Is there a possibility to always display the latest child? In this case:

Service page would show: Service, I click on Nachwuchsförderungen, it should show category nachwuchsfoerderungen (and not service), If I click on Entwicklung, it should show category entwicklung (and not service)...

This code is in functions.php

function cnext_event_cat() {
    static $parent_cat;
    if(! isset($parent_cat))
        $parent_cat = get_category_by_slug('events');
    foreach(get_the_category() as $cat)
        if($cat->category_parent == $parent_cat->cat_ID)
            return $cat;
}
1
  • what are you using to determine which category is "first" and which one is "last"? Do you actually mean the "deepest"? Also the cnext_event_cat only returns something in some situations, which is a bug, there is no fallback or default. I also visited the site and couldn't figure out what the grid referred to as everything was in german. I also noticed it commits the cardinal sin of not using hyperlinks to implement hyperlinks. Links should be <a> tags, not <div> tags, this is a major accessibility issue. Can you edit the Q to include more code and hard examples I can visit and see?
    – Tom J Nowell
    Commented Dec 14, 2022 at 16:48

1 Answer 1

0

if I understand, you need to display category or taxonomy last child. I think you should use get_terms function for this matter: Get all first childrens from your taxonomy:

$my_childrens_firsts = get_terms( array('taxonomy' => 'your_taxonomy', 
                'hide_empty' => false, 
                'hierarchical' => true,
                'parent'=> $your_cat_id,
                ) );

Now you have to foreach $my_childrens_firsts (foreach($my_childrens_firsts as $key => $first_children)... so you get your $first_children->term_id

still within this foreach and get next childs level, using again get_terms:

$my_childrens_seconds = get_terms( array('taxonomy' => 'your_taxonomy', 
                'hide_empty' => false, 
                'hierarchical' => true,
                'parent'=> $first_children->term_id,
                ) );

and so on...until the sublevel you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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