ID); // comma-separate posts $separator = ', '; // create output variable $output = ''; // if there are categories for the post if($categories){ echo '

'; foreach($categories as $category) { // if it's the last and not the first (only) category, pre-prend with "and" if( $category === end($categories) && $category !== reset($categories) ) { $output .= 'and '; } // output category name linked to the archive $output .= 'name ) ) . '">'.$category->cat_name.''.$separator; } echo trim($output, $separator); echo "

"; }