Posts Tagged ‘jquery’

jQuery Wordpress quick collapsable menus

Just a quick code snippet for creating Collapsible menus for the WordPress Page and Category Sidebar widgets.  It uses jQueryGoogle Ajax libraries and in its current state works only when you set your WordPress permalinks to /%category%/%postname%/.

Warning: By no means is this the best way to make your WordPress Page and Category Sidebar widgets expandable but it worked for me so I figure i’d share it.

google.load("jquery", "1");
google.setOnLoadCallback(function() {
jQuery("li.page_item a,li.cat-item a").each(function(){
	if(jQuery(this).next('ul').length){
		jQuery(this).after('&nbsp;&nbsp;&nbsp;&nbsp;<a class="toggle_list" href="#">+</a>');
	}
});
 
jQuery("li.page_item > ul,li.cat-item > ul").hide();
jQuery("a.toggle_list").click(function(){
	if(jQuery(this).next('ul:visible').length){
		jQuery(this).html('+').next('ul').hide('fast');
	}else{
		jQuery(this).html('–').next('ul').show('fast');
	}
});
var h = window.location.href;
jQuery('li.page_item a[href='+h+']').nextAll('ul').show();
h=h.substr(0,h.lastIndexOf('/'));
jQuery('li.page_item a[href='+h+']').nextAll('ul').show();
jQuery('li.page_item a[href='+h+']').next('a').html('–');
}

This will add a plus sign to the end of any parent pages and collapse them once the jQuery library is downloaded;  Your basic open and closing happens when you click the plus sign.  It should also open up the menu section if you are in the section or a child section.  Also this hasn’t been testing on a parent > child > child scheme.

Removing widows

After reading an article from CSS Tricks: Preventing Widows in Post Titles I decided it was about time to make my first jQuery plugin, the Widow Remover.

The Problem: A single word alone on it’s own line.

The Solution: add in a non-breaking space


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(function($) {
	$.fn.widow_remover = function(params) {
		params = $.extend( {words: 1}, params);
 
		this.each(function() {
			var string = $(this).text();
			var sp = string.lastIndexOf(' ');
			if(sp != -1){
				do{
					string = string.slice(0,sp)+' '+string.slice(sp+1);
					params.words -=1;
					sp = string.lastIndexOf(' ');
				}while(params.words&gt;0 &amp;&amp; sp != -1);
			}
			$(this).html(string);
		});
		return this;
	};
})(jQuery);

Usage:

1
jQuery('h1').widow_remover();

Result:

No more single words on a line

Still needs some improvements as looking back on it parts of the code I think I was in daze but the basic idea is there and hopefully it helps someone out

The Code is hosted at code.google.com and at plugins.jquery.com

Free Sprint phones for sale | Thanks to CD Rates, Reverse Phone and Registry Software