Category name by id
 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 Category ID.
<?php echo get_cat_name(4);?>
Here get_cat_name(4) is my Selected Category ID. Please Change 4With you 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>
	$catquery = new WP_Query( 'cat=4&posts_per_page=10' );
	while($catquery->have_posts()) : $catquery->the_post();
    <a href="<?php the_permalink() ?>" rel="bookmark">
     <?php the_title(); ?>
  <?php endwhile; ?>
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.

Tags: category name by id, category name by id in wordpress, wp get category name by id, get category id by name, wordpress get posts by category name, wordpress current category name

Post a Comment