Showing Category Link and Category name inside a loop

In case where you want to show both category name and category link inside a loop, here is the block of code for you.

For example you want to show list of blogs and also the category name and link that particular blog belongs to.

 <?php global $post; query_posts('post_type=post&posts_per_page=-1&paged='. get_query_var('paged')); //some random query $cat_nam = get_the_category($post->ID);
    $cat_link = get_category_link($cat_nam[0]->cat_ID); // dispaly category link
    $cat_name = $cat_nam[0]->cat_name; // dispaly category name
?>
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>
    <a href="<?php echo $cat_link;?>"><?php echo $cat_name;?></a> 
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

About Author

I am Robin - WordPress Developer based in Sydney. I work as Digital Developer. I do some projects now and then, and love blogging about WordPress and Web Design.