Get Category Name by ID WordPress


If you want to create a newspaper site or blog site like newspaper you need to display post base on category. Here I will add a WordPress snippets which will display category name by ID.

Get Category name by id

<?php echo get_cat_name(4);?>

Here get_cat_name(4) is my Selected Category ID. Please Change  4 With your Category ID.

Category link with Category ID

The above WordPress snippets will display only Category name. If you want to add link of the category you need to add below snippets.

<?php echo get_category_link(4); ?>

Here get_category_link(4) will display category link by Category ID. Please Change 4 With you Category ID.

Category name with category link by ID

The above two snippets will display Category name and category link separately by category id. I only added the above two codes as a link.

<a href="<?php echo get_category_link(4); ?>"><?php echo get_cat_name(4);?></a>

A small project of above snippets

Simply add the below WordPress snippets where you want to display post from specific category. If your sidebar widgets support php code, You can add it into you sidebar using text widgets.

<h2><a href="<?php echo get_category_link(4); ?>"><?php echo get_cat_name(4);?></a></h2>
<ul>
  <?php
	$catquery = new WP_Query( 'cat=4&posts_per_page=10' );
	while($catquery->have_posts()) : $catquery->the_post();
	?>
  <li> 
    <a href="<?php the_permalink() ?>" rel="bookmark">
     <?php the_title(); ?>
    </a>
  </li>
  <?php endwhile; ?>
</ul>

The above code will display 10 post for you category. You need change category ID.

If you have any question or problem about the snippets. Don’t forget to ask me using comment section. If this post is helpful don’t forget to share.

Masum Billah has written 326 articles

Hi I am Md. Masum Billah Contributor of this website. I always like to share various things with other. In this website I am sharing web design related tips and tricks, necessary web development tools, source codes for beginner and advance web developer. You may join with us to help other who want to learn web design and development. Thank you for stay with.

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>