Codeable info

Four Very Useful WordPress Snippets

Posted on by in WordPress Tutorials

Each of these snippets are extremely useful for accomplishing a variety of tasks.

Get Page ID by Page Name

1
2
3
4
5
6
function get_ID_by_page_name($page_name)
{
	global $wpdb;
	$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
	return $page_name_id;
}

You can use this function in scenarios where you want to display link to a particular page, but would prefer that WordPress generate the link for you, rather than just typing it out yourself (never a great idea).

For example, maybe you want to link to your about page:

1
2
$page_id = get_ID_by_page_name('About');
wp_list_pages('include=' . $page_ID); // displays a link to your about page

Check if Current Page is Page or Descendant of Page

Another useful trick is being able to determine whether or not a particular page is either a specific page, or a descendant of that page.

This function will check if the specified page ID is a member of the tree:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function is_tree($pid)
{
	global $post;         // load details about this page
	$anc = get_post_ancestors( $post->ID );
	foreach($anc as $ancestor) {
		if(is_page() && $ancestor == $pid)
		{
			return true;
		}
	}
	if(is_page()&&(is_page($pid)))
               return true;   // we're at the page or at a sub page
	else
               return false;  // we're elsewhere
 
}

Then use the function like this:

1
2
3
4
if ( is_tree('4') )
{
	// any codes here will be executed if the page ID 4 is in the tree
}

Get the URL of the Featured Thumbnail

Sometimes, such as when you wish to display the featured image in a lightbox, in can be necessary to obtain the URL of the Featured Thumbnail set for the current post or page. Using this function, you can do just that:

1
2
3
4
5
// store the image URL in an array
$image = wp_get_attachment_image_src( get_post_thumbnail_id(  $post->ID ), "thumbnail-size" );
 
// output the URL
echo $image[0]

The variable $image[0] is where the URL resides, so by echoing it out, you can display the image URL. You can also control the size of thumbnail the URL points to by changing the “thumbnail-size” parameter to either an image size added with add_image_size(), or to an array like this:

1
2
// get URL for 400 x 300 image
$image = wp_get_attachment_image_src( get_post_thumbnail_id(  $post->ID ), array(400, 350 true) );

Get Category Name from Category ID

Similar to our first function, except in reverse, and for categories, this function will allow us to return the name of a category with a specified ID.

1
2
$category_id = 12; // replace with your category ID
echo get_cat_name($category_id)

This function is also useful for scenarios, such as option panels for themes and plugins, where a user has specified ID numbers for categories, and you want to display the name of those categories in your template.

Enjoy!

)
Codeable info

Comments (2)

Comment by Pippin Williamson says:

Thanks Michael. I will be sure to do that.

Comment by Michael Fields says:

Number 1 is not very secure. $wpdb->prepare() should be used for all custom queries.

Codeable info