Menu icon Foundation
Wordpress - displaying parent and child categories

Hey!

Alright, so I will admit that this is only LOOSELY related to Foundation - I am using the FoundationPress Wordpress theme (https://github.com/olefredrik/foundationpress) and have run into an issue. The users here are awesome so I thought I'd ask my question here, although this specific problem isn't actually to do with Foundation. If that's not appropriate, I apologise and will close the thread...

I have created a custom post type - "Machines". Within that custom post type, I have categories - "Machine Type". I have entered machine types, something like this:

- Boom Lift
- Scissor Lift
- Cranes

I was displaying those categories on the site easily enough, but now I have created child categories, for example:

- Boom Lift
- Scissor Lift
- Electric Scissor Lifts
- Diesel Scissor Lifts
- Cranes

I'd like a way to easily display this information, but have no idea how, AND it seems like there's little information about how to do this online! The code I was using to display the original category information doesn't let you seperate parent and child categories, so with the example above it would just dump Scissor LiftElectric Scissor Lifts.

I've tried various tutorials and plugins, but no success. Now I'm throwing myself at the mercy of this forum, even though I know it's not what it's for, there are still some really smart and knowledgeable people and maybe one of you can help.

Thank you!!

wordpressfoundationPress

Hey!

Alright, so I will admit that this is only LOOSELY related to Foundation - I am using the FoundationPress Wordpress theme (https://github.com/olefredrik/foundationpress) and have run into an issue. The users here are awesome so I thought I'd ask my question here, although this specific problem isn't actually to do with Foundation. If that's not appropriate, I apologise and will close the thread...

I have created a custom post type - "Machines". Within that custom post type, I have categories - "Machine Type". I have entered machine types, something like this:

- Boom Lift
- Scissor Lift
- Cranes

I was displaying those categories on the site easily enough, but now I have created child categories, for example:

- Boom Lift
- Scissor Lift
- Electric Scissor Lifts
- Diesel Scissor Lifts
- Cranes

I'd like a way to easily display this information, but have no idea how, AND it seems like there's little information about how to do this online! The code I was using to display the original category information doesn't let you seperate parent and child categories, so with the example above it would just dump Scissor LiftElectric Scissor Lifts.

I've tried various tutorials and plugins, but no success. Now I'm throwing myself at the mercy of this forum, even though I know it's not what it's for, there are still some really smart and knowledgeable people and maybe one of you can help.

Thank you!!

Chris M over 5 years ago

Update on this problem: I've finally found some code which seems like it's tackling the right problem:

 <?php 
// get top level terms
$parents = get_terms( 'machine_type', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'machine_type' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '<a href="' . get_term_link( $parent ) . '">' . $parent->name . '</a>: ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
    if( $parent->term_id == $category->parent ):
        // put link in array
        $links[] = '<a href="' . get_term_link( $category ) . '">' . $category->name .      '</a>';
    endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;

?> 

However, what it outputs isn't quite right. For example, on a machine which is categorised as a Boom Lift, and also an Articulated Electric Boom Lift, it's now displaying: "Boom lift: Articulated Booms (electric)Scissor lift: Spider-lift:"

So it's adding on 2 other categories which aren't applicable to this machine. Any ideas?