• Services
    • Website Design & Development
    • E-Commerce Shopping Cart Development
    • Website Management
    • Web app & website integration
    • School Website Design
    • Competition Website Design
  • Portfolio
  • Testimonials
  • Contact
  • Blog
  • 01298 816080
  • Menu Menu
You are here: Home1 / Code Snippets2 / PHP3 / Highlight Current Post Category In WordPress Categories Widget Using jQuery...

Highlight Current Post Category In WordPress Categories Widget Using jQuery PHP

Code Snippets, PHP, Wordpress

This simple snippet of PHP code will apply the ‘current-cat’ class to any categories in the WordPress categories widget that the current post resides in.

Add this code to your single.php template file in your current template. Make sure you have a .current-cat class in your css.

<?php
$post_categories = wp_get_post_categories( $post->ID ); 
$script_out = '';

foreach($post_categories as $c):
	$cat = get_category( $c );

	$script_out .= "$('.cat-item-".$cat->cat_ID."').addClass('current-cat');";

endforeach;
echo '<script>'.$script_out.'</script>';

?>

This assumes jQuery is included and uses $ to declare it.

  • Share:
  • Facebook
  • Twitter
  • Pinterest
  • Linkedin
  • Google+
April 5, 2014/by Martin Toner Web Designer Developer
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Vk
  • Share on Reddit
  • Share by Mail
https://sk23.co.uk/wp-content/uploads/2012/11/logo-h80.jpg 80 349 Martin Toner Web Designer Developer https://sk23.co.uk/wp-content/uploads/2019/09/sk23logo.svg Martin Toner Web Designer Developer2014-04-05 10:46:432014-04-05 10:50:38Highlight Current Post Category In WordPress Categories Widget Using jQuery PHP

Pages

  • About
  • Blog
  • Competition Website Design
  • Contact
  • E-Commerce Shopping Cart Development
  • Home
  • Magento CE E-Commerce Shopping Cart Web Development
  • Payment Confirmation
  • Payment Failed
  • School Website Design
  • Serious Coding
  • Simple Unlimited Page Website
  • Testimonials
  • Thanks
  • Web app & website integration
  • Website Management
  • Website Design & Development
  • Buxton Web Designer + Developer
  • High Peak Web Designer
  • Recent Web Design Projects Portfolio

Categories

  • Code Snippets
  • Design
  • Fun Stuff
  • Latest Work
  • Magento
  • Magento Guides
  • Magento Modules
  • MySql
  • Our Work
  • PHP
  • PHP Development Work
  • Website work.
  • Wordpress

Archive

  • February 2024
  • February 2023
  • October 2022
  • June 2022
  • August 2019
  • December 2016
  • July 2014
  • April 2014
  • September 2013
  • June 2013
  • March 2013
  • February 2013
  • October 2012
  • May 2012
  • February 2012
  • January 2012
  • October 2011
  • September 2011
  • February 2011
  • January 2011
  • October 2010

Contact Us

If you are looking for a new website , e-commerce store, web application or just some advice please get in touch.

info@sk23.co.uk 01298 816080

Address

SK23Design
Suite 1,
4 Market St,
Chapel-en-le-Frith,
High Peak SK23 0HH

Partners

Recent

  • Magento 2.4 – Why Your Custom Theme’s Css Is Not Being Compiled
  • Magento 2.4 – Change Product Details From Tabs to Accordion On Desktop
  • Magento 2.4 Bulk Updates Not Working Or Appearing In The Action Log
  • Magento 2.4 Disable Page Builder For Category Description
  • Magento 2 UK Shipping

Categories

  • Code Snippets
  • Design
  • Fun Stuff
  • Latest Work
  • Magento
  • Magento Guides
  • Magento Modules
  • MySql
  • Our Work
  • PHP
  • PHP Development Work
  • Website work.
  • Wordpress
  • Web Design High Peak
  • Web Design Buxton
  • Web Designer Chapel-en-le-Frith
  • Loqate
2026 © Copyright - SK23 Design IT - Websites and Web Development Services | Website by SK23Design
WordPress Spam BuddyPress Easy MySQL Spam Filter QueryMagento Delete All Tables Prefixed With ‘wp_’ Scroll to top