phpwoocommercecategories

WooCommerce - show product child category from specific parent with permalink & html


With regards to this question which was nicely solved:

WooCommerce - show product child category from specific parent

I managed to get it working with this code:

dd_action( 'woocommerce_single_product_summary', 'wpse124955_test', 99 );
function wpse124955_test() {
$taxonomy = 'product_cat';

$term_id = wp_get_post_terms(get_the_ID(), $taxonomy, array("fields" => "ids"));

$parent = '21';
$args = array(
    'fields' => 'ids',
    'child_of' => $parent
);
$branch_ids = get_terms( $taxonomy, $args );

$intersect_ids = array_intersect($term_id, $branch_ids);

foreach ( $intersect_ids as $tid ) {
    $tobj = get_term_by('id', $tid, 'product_cat');
    $name_arr[] = $tobj->name;
 }

$term_list = implode(', ', $name_arr);

echo $term_list;
}

The child category gets printed nicely with this code.

Now I would like to have the child category linked (to itself) and also enclosed with a

<h1></h1>

so that i can style it with my css, but being a complete PHP idiot i have no idea how to edit the code above.

Have trawled through the internet for hours without a straightforward answer. This was the best solution - just needs some tweaking! If anyone would help i would be eternally grateful! Thank you!


Solution

  • $name_arr[] = $tobj->name;
    

    to

    $name_arr[] = '<a href="'.get_permalink( $tid ).'"><h1>'.$tobj->name.'</h1></a>';
    

    should work.