WordPress code for highlighted category links

Last modified on:

If you want to display a higlighted category to your  visitors so that they know where they are in the site, use the following code, which checks to see if the page is of a certain name, category or if in the case that a single post is being displayed, it checks if one of the categories matches ‘id#’ (the number of the category you want to specify). If any of these is true, the class tag is written into the page.

Add the code into <li> or <a> or <h2> tags that surround the call for the category list or into the hard coded links (which is what I use in my own site).

<?php
if ( is_page(‘pagename‘) ) {
echo ‘class=”active”‘;
} elseif ( is_category(‘id#‘) ) {
echo ‘class=”active”‘;
} elseif ( in_category(‘id#‘) ) {
echo ‘class=”active”‘;
} else {
//
}
?>

Once you have this in place, all you need do is create a corresponding class in your css file to highight the text or image link and voila.

After the last ‘//’ you could have class=”unactive” or whatever name you choose to use in order to style your navigation link to suit your site.


Published on

in

by

Tags:

Leave a Reply

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