WordPress Output Post Categories

Glenn Glenn's Blog Leave a Comment

Here is a snippet to output the current categories of a post in the Post Archive or on your single Post:-


<?php
$categories = get_the_category();
$separator = ', ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
?>

This displays the posts with a link to the category archive delimited by a comma – edit line 3 to update the separator which is trimmed off on the last category,

About the Author
Wordpress Output Post Categories Wordpress Output Post Categories 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced Web Designer / Web Developer (Webflow, WordPress, Weebly & Custom), Front-end / Back-end Developer & New Media Specialist, with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries, built up over a number of years.