Skip to content

Instantly share code, notes, and snippets.

@marushu
Created August 22, 2014 18:41
Show Gist options
  • Save marushu/e7d579d083ac37ae2f34 to your computer and use it in GitHub Desktop.
Save marushu/e7d579d083ac37ae2f34 to your computer and use it in GitHub Desktop.
タームのパンくず。get_ancestorsで辿る感じ。sortで良いのかわかんないんだよなぁ…(^0^;)
<?php
$test = wp_get_post_terms($post->ID, 'product_cat' );
if( ! empty( $test ) && ! is_wp_error( $test ) ) {
$test_2 = wp_get_object_terms( $post->ID, 'product_cat' );
$last_cat_link = get_term_link( $test_2[0]->term_id, 'product_cat' );
$last_cat_name = $test_2[0]->name;
$last_cat_tag = $last_cat_name;
foreach( $test_2 as $var ) {
$ans = get_ancestors( $var->term_id, 'product_cat' );
foreach( $ans as $an ) {
$an_link = get_term_link( $an, 'product_cat' );
$an_obj = get_term_by( 'id', $an, 'product_cat' );
$an_name = esc_attr( $an_obj->name );
$term_tag = sprintf(
'<a href="%1$s">%2$s</a><br />',
$an_link,
$an_name
);
//echo $term_tag;
}
}
sort( $ans );
foreach( $ans as $an ) {
$an_link = get_term_link( $an, 'product_cat' );
$an_obj = get_term_by( 'id', $an, 'product_cat' );
$an_name = esc_attr( $an_obj->name );
$term_tag = sprintf(
'<a href="%1$s">%2$s</a>',
$an_link,
$an_name
);
echo ' &gt; ' . $term_tag;
}
echo ' &gt; ' . $last_cat_tag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment