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

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

I am wordpress developer and web designer based in sydney.

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

Related Post

WordPress Developer Sydney
For Hire

If you are looking for quality top notch WordPress Web Developer, look no further. I have strong attachment with all projects I have done, and can guarantee I have same love and feelings to your website as yourself. Because I believe that the project I deliver reflects me as WordPress Web Developer.

Get a Free Quote