Today I had a need to create a list of WordPress child category links in a theme. Each category in the list needed a pipe divider, except the last one. So my categories will look like:
I looked into using wp_list_pages, but that didn’t quite get the job done. So, here is what I came up with:
<strong>Categories: </strong> <?php $categories= get_categories('child_of=12'); $total_number_of_categories = count ($categories); $i = 1; foreach ($categories as $category) { $category_name = $category->cat_name; $category_ID = $category->term_id; $category_link = get_category_link ($category_ID); echo '<a href="'. $category_link .'">' . $category_name . '</a>'; if ( $i < $total_number_of_categories ) { echo ' | '; } $i++; } ?> |
Is there a better way to do this?
Worked like a charm 🙂 Thanx!