Please Share:

Showing Category Link and Category name inside a loop

By: Robin Thebe Posted on: Wordpress Development Tips Dated: December 5, 2016

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(); ?>
wordpress web developer, web design sydney
Robin Thebe

I am wordpress web developer and web designer based in sydney.

I focus on web design and creating custom wordpress development, optmised for SEO and Speed.

Related Articles

Got a project in mind

I have helped small, medium sized busines and corporates with their online presence.

I would love to hear from you if you need any help regarding web design and development.

Get a Free Quote