WordPress – Get category name in sidebar

I wanted to add some code in the sidebar depending on the category name of the post and also display the category name.

$categories = get_the_category($post->ID);
	if ($categories) {
		$category_ids = array();
		foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
			if($individual_category->name == 'Category First' || $individual_category->name == 'Category Second'){
				echo $individual_category->name;
                                echo "Whatever you want to do here";
			}

So here, if you were to do something if the category was lets say “cooking”. Then just replace in the if condition and do whatever you want inside the if condition.

You can play around with this more. For any other queries, leave me a comment.

Thanks

Tags: , ,

Leave a Reply

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

*
*