612-293-8629 HELLO@THEMIGHTYMO.COM

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:

Category 1 | Category 2 | Category 3

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?

PHONE, EMAIL, TEXT

We love to communicate: phone, email, text, Facebook, Twitter, LinkedIn, you name it.

LOCATIONS

PAYMENTS ACCEPTED

Call Now

Enroll in Our Free Beginner WordPress Class - delivered to your Email Inbox!

Become a WordPress superstar for FREE in only minutes a day!  Our free Beginner WordPress Email Class will be delivered to your inbox!

Learn More 

You Have Successfully Enrolled! Prepare For Amazing!