Reputation: 31
So I'm reasonably new to Wordpress, but have been able to solve most issues by myself up until now.
I have a custom post type - lets call it Machines
. And in those machines, there is a category called Machine_type
. And say I've made a machine type called Scissor Lifts
, then another machine type called electric scissor lifts
which is a child of scissor lifts
.
I'd like to display this information on the single post page - so on the electric scissor lifts
page, i'd like to display breadcrumbs like Machines - Scissor Lifts - Electric Scissor Lifts. But this seems almost impossible! I have tried using many different tutorials, but they seem to always display Machines - - - Final machine name
, without the actual 2 categories I'd like to display!
It seems sort of crazy that there's no simple inbuilt call for Category and Child Category! I was using this:
<?php
$terms = get_the_terms( $post->ID , 'machine_type' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'machine_type' );
if( is_wp_error( $term_link ) )
continue;
echo '<p>Machine Type: <a href="' . $term_link . '">' . $term->name . '</a></p>';
}
?>
But this obviously just dumps all the information out, no way to separate the parent and child categories nicely.
EDIT:
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?
Upvotes: 0
Views: 4880
Reputation: 248
$wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
echo $wcatTerm->name;
$wsubargs = array('hierarchical'=>1,'show_option_none'=>'','hide_empty'=>0,'parent'=>$wcatTerm->term_id,'taxonomy'=>'product_cat');
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
echo $wsc->name;
endforeach;
endforeach;
Use this it's display two level of category name and product name.
Upvotes: 3
Reputation: 1
This may help yo
<?php
/********************** List out the parent and child inside parent ***/
$taxonomyName = "your_taxonomy";
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 1) {
wp_list_categories('taxonomy=your_taxonomy&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=your_taxonomy&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
Upvotes: 0