How To Highlight Current Category In WordPress Menu

The new menu manager in WordPress 3.0+ is a great addition, and something that is extremely useful for clients. However in certain spots it’s not very intuitive how you get the current page/post/object highlighted.

One of those scenarios is on single post pages. By default the new menu manager will output a class of “.current-menu-item” for a category navigation link only when you are  actually navigating a category landing page, and not on single post pages assigned to that category.

I figure I could approach the solution to this in two ways. I  could try to modify the output of wp_nav_menu itself via the WordPress API or I could do it after the fact, on the client side via jQuery.

From what I’ve read regarding the first option, there actually aren’t many places to hook into this menu and thus may require the use of my own “walker class.”

Using a walker class is a new concept to me, so this probably would require a fair amount of work. Much more than I was willing to do just to highlight the current category. A far easier solution in my opinion is just to go with  the jQuery option and do this on the front-end.

So I came up with this quick solution…

1.  Link up jQuery in your WordPress header.php template, if not already there…

1
2
3
4
5
6
7
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    // Your jQuery  here
  });
</script>

2. On your single post template (single.php) dump out the category name somewhere in the HTML (that later can be picked up with jQuery).

In my case I added a “title=” attribute to the #post-xx DIV.

1
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> title="<?php $category = get_the_category(); echo $category[0]->cat_name; ?>">

Just to be clear, the part that I added was…

1
title="<?php $category = get_the_category(); echo $category[0]->cat_name; ?>

I should note, this is actually only dumping out the first category (a WordPress post can have multiple categories of course). The way I  setup sites around categories, usually there is a 1:1 relationship between posts and categories.

If you did have  multiple categories for a post, I’m not sure highlighting multiple “current categories” would be that  important to you (perhaps you’re better off leaving it how it is).  If you really do want to highlight multiple categories in the navigation, this same concept will work, but you’ll have to tweak the code a bit.

3. Use jQuery to store the category name as a variable and then apply the appropriate class in the navigation.

1
2
3
4
5
6
7
8
$(document).ready(function() {
var active_cat_name = $("body.single div.post").attr("title");
$("ul.menu li.menu-item-object-category a").filter(function(index) {
 return $(this).text() == active_cat_name; }).parent().addClass("current-menu-item");
});

Let’s look at each line separately…

1
var active_cat_name = $("body.single div.post").attr("title");

Here we are setting the variable “active_cat_name” equal to the assigned category name from the “title=” attribute we dumped into the HTML in step #2.

Note the use of “body.single div.post” to specifically target the appropriate DIV on single pages. Depending on how your templates are setup, the actual selector my be slightly different.

1
2
$("ul.menu li.menu-item-object-category a").filter(function(index) {
 return $(this).text() == active_cat_name; }).parent().addClass("current-menu-item");

In this line we are selecting any menu generated by wp_nav_menu (ul.menu) and scanning the anchor text for the category name. If there is a match the class of “current-menu-item” is added to the parent list item.

Note the use of “li.menu-item-object-category” in the selector which limits this to only categories in the navigation.

4. Create styles in your CSS file to highlight the current state

1
.current-menu-item { background-color: red; }

And you’re done!

One quick caveat: This does a match via the “category name” because the wp_nav_menu does not output the category ID. Technically, in WordPress, it is possible to have more than one category with the same name (and different slugs). A better solution may be to use the same concept but in step 2 dump out the category slug into the HTML , and then in step 3, assuming permalinks are on, scan the anchors href instead of the anchors text for a match.

How to add Facebook like box as sliding panel

Please add this code in your theme’s index.php before get sidebar
<pre lang=”php”><script>// <![CDATA[
(function(d,s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = “//connect.facebook.net/en_US/all.js#xfbml=1”;
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));

$(document).ready(function () {
bottompx = “0px”;
$(‘#facebook_likebox’).slideDown(1500).css({bottom: bottompx});
//facebook likebox
});
function closefaceboxlike(){
$(‘#facebook_likebox’).slideUp(1500);
}
// ]]></script></pre>
<div id=”facebook_likebox” style=”float: right; position: fixed; right: 0px; margin-bottom: 10px; border: 2px solid #cddbe4; background-color: #e7ecf2; z-index: 10; bottom: 0px;”>
<div style=”position: absolute; left: -10px; margin-top: -10px; margin-right: 174px; z-index: 15;”><a> <img src=”http://paimages.prothom-alo.com/secured/theme/public/ newdesign/style/images/closebox.gif” alt=”” height=”20″ /> </a></div>
<div class=”fb-like-box” data-header=”false” data-stream=”false” data-show-faces=”false” data-height=”80″ data-width=”292″ data-href=”http://www.facebook.com/0lpokotha“></div>
</div>

http://www.facebook.com/<del datetime=”2012-12-07T11:56:09+00:00”>0lpokotha</del>
Change your facebook page name “olpokotha” to your’s  .

blog’s sticky posts

Looking at your site’s source, I doubt you’ll be able to achieve the what you’re looking for purely through CSS. This means editing the PHP files. The file will most likely be index.php, or if it’s a newer theme, it may be in loop-index.php or loop.php. Without taking a look at the code, it’s hard to be more specific. Regardless, you’ll see something like:

<?php while( have_posts() ) : the_post(); ?> 
<div id="post-<?php the_ID(); ?>"
 <?php post_class(); ?>> ... </div>
 <?php endwhile; ?> 

When you find this section, add the following in between the <div> tags:

<?php if ( is_sticky() ) : ?> <p class="sticky-banner">New Product!</p> <?php endif; ?> 

 

Protact your post

To protect your posts please write this code in wordpress theme’s index.php file or header.php  before

/header

<script type=”text/javascript”>// <![CDATA[
function mousedwn(e)
{
try { if (event.button == 2||event.button == 3) return false; }
catch (e) { if (e.which == 3) return false; }
}

window.oncontextmenu = function(event) {
event.preventDefault();
event.stopPropagation();
return false;
};

document.oncontextmenu = function() { return false; }
document.ondragstart = function() { return false; }
document.onmousedown = mousedwn;

// ]]></script><script type=”text/javascript”>// <![CDATA[
window.addEventListener(“keydown”,function (e) {
if (e.ctrlKey && (e.which == 65 || e.which == 67 || e.which == 85 || e.which == 80)) {
e.preventDefault();
}
})
document.keypress = function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 67 || e.which == 85 || e.which == 80)) {
}
return false;
};
// ]]></script><script type=”text/javascript” language=”JavaScript”>// <![CDATA[
if (top.location != self.location) top.location.replace(self.location);
// ]]></script><meta http-equiv=”imagetoolbar” content=”no” /><script type=”text/javascript”>// <![CDATA[
document.ondragstart = function(){return false;};
// ]]></script></pre>
<style type=”text/css”><!–
* {
-webkit-touch-callout: none;
}

img {
-webkit-touch-callout: none;
}
–></style>