I am using this snippet (referenced from here) to seperate categories in my post meta.

<?php
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(7, $childcat)) {
echo $childcat->cat_name;
}}
?>

The thing I need to work out is how to place an ' | ' between each link (but preferably not the last one) and also to make them links to their specific categories (it just outputs the category name's text at the moment).

Anyone able to shed some light?

1 answer

0
points

Managed to work it out myself. It is dependent on your permalink structure setup being pretty but here is a way to link to those particular category's.

<?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(14, $childcat)) { echo '<a href="/category/',$childcat->category_nicename,'">',$childcat->cat_name,'</a>', ' '; }}
?>

You basically need to set the /category/ part to whatever your permalink structure is as the output of category_nicename is the URL friendly version of the particular category's name.

Answered over 7 years ago by Michael Cairns