612-293-8629 [email protected]

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>
$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 ' | ';
} ?>

Is there a better way to do this?


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



Call Now

Enroll in Our Free Beginner WordPress Class by Email

Never feel powerless in WordPress again!  Become a WordPress superstar for free in only minutes a day!

Our Beginner WordPress Email Class is delivered to your inbox!

Learn More 

You Have Successfully Enrolled! Prepare For Amazing!