Oneupweb : Useful WordPress Functions

During the development of many WordPress themes, we’ve found that certain functions which seem necessary were never created for the core library. This is where the functions.php file comes in handy. By adding code to the file, you can call written functions within your themes as though they’re part of WordPress itself.

Here are some functions that we wrote over the past several months to help speed up theme development…

We should probably mention that most of these functions were built to retrieve information from a database. Although we stand by our work, we must mention that there is no guarantee that they will function as intended. The authors, and companies for which they work, cannot be held liable for any damage that may occur, directly or indirectly, from the following scripts. Use at your own risk!


Page Functions

is_subpage()

Determines if a page is a subpage, or is the child/descendant of a particular page.

// is_subpage() - determine if the current page has a parent
// is_subpage('parent') - match the parent against a title, slug or ID
// is_subpage('parent', true) - will climb the family tree until a match is found or it reaches the top
function is_subpage($elder='',$ascend=false)
{
	global $post;

	if( is_page() && $post->post_parent ) {
		$parent = $post->post_parent;
		switch($elder){
			case '':
			case $parent:
			case get_post_slug_by_ID($parent):
			case get_post_name_by_ID($parent):
				return true;
		}
		if($ascend){
			return is_descendant(get_page_ID($elder), $post->ID);
		}
	} else {
		return false;
	}
}

// a helper function
// climbs the parent tree when the 2nd parameter in is_subpage() is set to true
function is_descendant($elder, $child)
{
	$parent = get_parent_ID($child);
	$result = ($elder==$parent);
	if(!$result && $parent){
		$result = is_descendant($elder, $parent);
	}
	return $result;
}

// a helper function
// retrieves the parent ID from the specified page
function get_parent_ID($id)
{
	global $wpdb;
	$query = "
		SELECT post_parent
		FROM $wpdb->posts
		WHERE ID = $id
		LIMIT 1;
	";
	$results = $wpdb->get_results($query, OBJECT);
	return $results[0]->post_parent;
}

get_parents()

Returns an array of post objects, each entry being a parent to the next.

For example, $result[0] would be the parent of $result[1] and so on.

function get_parents($child='')
{
	global $post;

	$child = preg_replace('/D/', '', $child) ? $child : $post->ID;

	$parents = array();

	$parent = get_post($child, OBJECT);
	array_unshift($parents, $parent);

	while($parent->post_parent){
		$parent = get_post( $parent->post_parent, OBJECT );
		array_unshift($parents, $parent);
	}

	return $parents;
}

Category Functions

An empty string will be returned if attempting to call either of the following functions from within a page:

get_category_slug()

Retrieve the category slug for the current archive/post.

function get_category_slug()
{
	if(is_category()){
		$cat = get_query_var('cat');
		$on_cat = get_category($cat);
		$slug = $on_cat->slug;
	} elseif(!is_page()) {
		$cat = get_the_category();
		$on_cat = $cat[0];
		$slug = $on_cat->slug;
	} else {
		$slug = '';
	}
	return $slug;
}

get_category_name()

Retrieve the category name for the current archive/post.

function get_category_name()
{
	if(is_category()){
		$cat = get_query_var('cat');
		$on_cat = get_category($cat);
		$name = $on_cat->cat_name;
	} elseif(!is_page()) {
		$cat = get_the_category();
		$on_cat = $cat[0];
		$name = $on_cat->cat_name;
	} else {
		$name = '';
	}
	return $name;
}

get_cat_ID_by_slug()

This is useful when WordPress functions require a category’s ID. Since ID’s will rarely be the same between blogs, using the slug can give you a little more control.

function get_cat_ID_by_slug($slug)
{
	global $wpdb;
	$query = "
		SELECT term_id
		FROM $wpdb->terms
		WHERE slug = '$slug'
		LIMIT 1;
	";
	$results = $wpdb->get_results($query, OBJECT);
	return $results[0]->term_id;
}