We're growing into something new. Read More
tammyhart

Tammy Hart


var        windowWidth;
	
function setWidth() {
	windowWidth = $( window ).width();
}
	
setWidth();
	
$( window ).resize( setWidth );
 
if ( windowWidth < 768 ) {
	...
}
 
if ( windowWidth > 320 ) {
	...
}

jQuery Media Queries

Set a null variable. Create a function that sets the var to equal the width of the browser window. Fire the function on load. fire it again on resize. Use the var to do conditional stuff. Boom.

// blog title
function blog_title() { 
	// $page is a post where post_type =='page'
	if (get_option('show_on_front') == 'page') {
		$page_id = get_option('page_for_posts');
		echo get_the_title($page_id);
	} else {
		echo 'Blog';
	}
}

Echo WordPress "Posts Page" title

If you use a static front page and another page as the "Posts Page", this function will echo the title of that page. This is great for adding a title to a page without hard coding it into a theme. The names users have for this page varies between things like "Blog", "News", "Updates", etc. This function simply captures the title that was set, and echos it. The backup if nothing is found is "Blog".

<?

// get children of three categories and merge them into one array
$cats1 = get_categories(array(
	'echo' => false,
	'child_of' => 9,
	'style' => 'none',
	'hide_empty' => 0,
	'order' => 'ASC'
));
$cats2 = get_categories(array(
	'echo' => false,
	'child_of' => 10,
	'style' => 'none',
	'hide_empty' => 0,
	'order' => 'ASC'
));
$cats3 = get_categories(array(
	'echo' => false,
	'child_of' => 11,
	'style' => 'none',
	'hide_empty' => 0,
	'order' => 'ASC'
));

$cats = array_merge($cats1,$cats2,$cats3);

// group categories alphabetically
foreach ($cats as $cat) :
	$af = array('a','b','c','d','e','f');
	$gl = array('g','h','i','j','k','l');
	$mr = array('m','n','o','p','q','r');
	$sz = array('s','t','u','v','w','x','y','z');
	$letter = substr($cat->slug, 0, 1);
	if (in_array($letter, $af)) $afcats[] = $cat->term_id;
	if (in_array($letter, $gl)) $glcats[] = $cat->term_id;
	if (in_array($letter, $mr)) $mrcats[] = $cat->term_id;
	if (in_array($letter, $sz)) $szcats[] = $cat->term_id;
endforeach;

// set up an easy way to call each group
function cats_af() {
	global $afcats;
	return $afcats;
}

function cats_gl() {
	global $glcats;
	return $glcats;
}

function cats_mr() {
	global $mrcats;
	return $mrcats;
}

function cats_sz() {
	global $szcats;
	return $szcats;
}

// set up the call to easily create a loop
function get_cats($set) {
	$cats = get_categories(array(
		'include' => $set,
		'echo' => false,
		'style' => 'none',
		'hide_empty' => 0,
		'order' => 'ASC'
	));
	
	return $cats;
}
?>

## Now create the output

<div id="cats-af" class="tab_content">
	<ul>
	<? $cats = get_cats(cats_af());
	foreach ($cats as $cat) { ?>
		<li><a href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
	<? } ?>
	</ul>
</div>
<div id="cats-gl" class="tab_content">
	<ul>
	<? $cats = get_cats(cats_gl());
	foreach ($cats as $cat) { ?>
		<li><a href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
	<? } ?>
	</ul>
</div>
<div id="cats-mr" class="tab_content">
	<ul>
	<? $cats = get_cats(cats_mr());
	foreach ($cats as $cat) { ?>
		<li><a href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
	<? } ?>
	</ul>
</div>
<div id="cats-sz" class="tab_content">
	<ul>
	<? $cats = get_cats(cats_sz());
	foreach ($cats as $cat) { ?>
		<li><a href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
	<? } ?>
	</ul>
</div>

Group WordPress Categories Into Alphabetical Groups

I needed to create a jQuery tab setup with childdren of categories 9, 10, and 11 in four alphabetical groups, a-f, g-l, m-r, and s-z. The first bit of code is for use in your functions file, and the output is for use on your template. Get the child categories and merge them into one array. Loop through the categories and sort them into the alphabetical groups Create functions for each grouping that can be called in the template Create a function that returns the categories in each group Output each group in their own div that can be used in the tab setup

<?

/* Post Type
   ------------------------------------------------------------------------------------ */
add_action( 'init', 'register_cpt_shot' );

function register_cpt_shot() {

    $labels = array( 
        'name' => _x( 'Dribble Shots', 'shot' ),
        'singular_name' => _x( 'Dribbble Shot', 'shot' ),
        'add_new' => _x( 'Add New', 'shot' ),
        'add_new_item' => _x( 'Add New Dribbble Shot', 'shot' ),
        'edit_item' => _x( 'Edit Dribbble Shot', 'shot' ),
        'new_item' => _x( 'New Dribbble Shot', 'shot' ),
        'view_item' => _x( 'View Dribbble Shot', 'shot' ),
        'search_items' => _x( 'Search Dribble Shots', 'shot' ),
        'not_found' => _x( 'No dribble shots found', 'shot' ),
        'not_found_in_trash' => _x( 'No dribble shots found in Trash', 'shot' ),
        'parent_item_colon' => _x( 'Parent Dribbble Shot:', 'shot' ),
        'menu_name' => _x( 'Dribble Shots', 'shot' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        
        'supports' => array( 'title', 'custom-fields' ),
        
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        
        'show_in_nav_menus' => false,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'shot', $args );
}   




/* Import Shots via RSS
   ------------------------------------------------------------------------------------ */

// grab the image src from teh description
function get_image($string) {
	preg_match_all('/<img[^>]+>/i',$string, $result);
	$img = array();
	foreach( $result[0] as $img_tag)
	{
		preg_match_all('/(src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
	}
	return trim($img[$img_tag][2][0], '"');
}

// create an array of the feed items
include_once(ABSPATH . WPINC . '/feed.php');
$feed = fetch_feed('http://dribbble.com/tammyhart/shots.rss');
$feed = $feed->get_items(0); 

$shots = array();
foreach ( $feed as $item ) : 
$shots[$item->get_date('Ymd')] = array(
	'id' 	=> $item->get_date('Ymd'),
	'url' 	=> esc_url( $item->get_permalink() ),
	'date' 	=> $item->get_date('Y-m-d H:i:s'),
	'title' => esc_html( $item->get_title() ),
	'image' => get_image($item->get_description())
	);
endforeach;

// create posts from our array
foreach ($shots as $shot) {
	$shot_post = array(
		'post_type' 	=> 'shot',
		'post_status' 	=> 'publish',
		'post_author' 	=> 1,
		'post_title' 	=> $shot['title'],
		'post_date' 	=> $shot['date']
		);
		
	$shot_post_meta = array(
		'link_url' 	=> $shot['url'],
		'image'		=> $shot['image']
		);
		
	$posts = get_posts( 
		array(
			'post_type'	=> 'shot',
			'meta_key'	=> 'link_url',
			'meta_value'=> $shot_post_meta['link_url']
			)
		);
		
	if (count($posts) == 0) {		
		$post_id = wp_insert_post($shot_post);
		add_post_meta($post_id, 'link_url', $shot_post_meta['link_url'], true);
		add_post_meta($post_id, 'image', $shot_post_meta['image'], true);	
	}
}
?>

Dribbble to WordPress

Create a custom post type 'shot' Grab Dribbble posts from an RSS feed and put them in an array Use the array to create 'shot' posts without duplicating them

<a href="
	{if alt_url}
		{alt_url}
	{if:else}
		{related_entries id="slide_destination"}
			{title_permalink}
		{/related_entries}
	{/if}
">Click here to read more</a>

Provide two options for a link destination in ExpressionEngine

I'm still pretty new to ExpressionEngine, but I'm proud of myself for figuring out this little snippet. In creating a homepage slider, I created a channel specifically for the slides. Most of the slides simply link to pages of a different channel. But one slide will periodically change content as it used to announce an event, and needs to link directly to that event's website. I used the relationships custom field to provide a dropdown list of all the pages as the slide_destination. Then in my conditional, the "else" is to grab the title_permalink of the page that is selected. The condition first looks to see if an alternate destination is provided, alt_url. Be sure to set both of these inputs to have a "Default Text Formatting" of None.